[Polly] Allow changing array dims when importing JScop (#174582)

This is in preparation to avoid relying on GEPs to derive dependency
information; Clang will eventually emit `ptradd` instead of
`getelementptr`.
This commit is contained in:
Michael Kruse 2026-01-06 14:07:03 +01:00 committed by GitHub
parent bdc7681d63
commit af3a0e61f4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 148 additions and 12 deletions

View File

@ -542,17 +542,6 @@ static bool areArraysEqual(ScopArrayInfo *SAI, const json::Object &Array) {
if (SAI->getName() != *Array.getString("name"))
return false;
if (SAI->getNumberOfDimensions() != Array.getArray("sizes")->size())
return false;
for (unsigned i = 1; i < Array.getArray("sizes")->size(); i++) {
SAI->getDimensionSize(i)->print(RawStringOstream);
const json::Array &SizesArray = *Array.getArray("sizes");
if (Buffer != SizesArray[i].getAsString().value())
return false;
Buffer.clear();
}
// Check if key 'type' differs from the current one or is not valid.
SAI->getElementType()->print(RawStringOstream);
if (Buffer != Array.getString("type").value()) {
@ -616,10 +605,45 @@ static bool importArrays(Scop &S, const json::Object &JScop) {
errs() << "Not enough array entries in JScop file.\n";
return false;
}
if (!areArraysEqual(SAI, *Arrays[ArrayIdx].getAsObject())) {
const json::Object &Array = *Arrays[ArrayIdx].getAsObject();
if (!areArraysEqual(SAI, Array)) {
errs() << "No match for array '" << SAI->getName() << "' in JScop.\n";
return false;
}
auto *DimSizeType = Type::getInt64Ty(S.getSE()->getContext());
const json::Array &SizesArray = *Array.getArray("sizes");
SmallVector<const SCEV *> SCEVSizes;
for (unsigned i = 0; i < SizesArray.size(); i++) {
std::string StrSize = SizesArray[i].getAsString()->str();
if (StrSize == "*") {
if (i != 0) {
errs() << "undefined size only allowed for outermost dimension\n";
return false;
}
SCEVSizes.push_back(nullptr);
continue;
}
int Size = std::stoi(StrSize);
// Check if the size if positive.
if (Size <= 0) {
errs() << "The size at index " << i << " is =< 0.\n";
return false;
}
const SCEV *ScevSize = S.getSE()->getConstant(DimSizeType, Size);
SCEVSizes.push_back(ScevSize);
}
// TODO: If changing the dimensionality of an array, all its accesses must
// be updated to that dimensionality. Currently there is no check
// whether all accesses are really updated.
if (!SAI->updateSizes(SCEVSizes, /*CheckConsistency=*/false))
return false;
ArrayIdx++;
}

View File

@ -0,0 +1,59 @@
; RUN: opt %loadNPMPolly '-passes=polly-custom<import-jscop>' -polly-import-jscop-postfix=transformed -polly-print-import-jscop -disable-output < %s | FileCheck %s
define void @change-array-dims(ptr noalias nonnull %A, ptr noalias nonnull %B) {
entry:
br label %outer.preheader
outer.preheader:
br label %outer.for
outer.for:
%j = phi i32 [0, %outer.preheader], [%j.inc, %outer.inc]
%j.cmp = icmp slt i32 %j, 2
br i1 %j.cmp, label %inner.preheader, label %outer.exit
inner.preheader:
br label %inner.for
inner.for:
%i = phi i32 [0, %inner.preheader], [%i.inc, %inner.inc]
br label %body
body:
%mul = mul nsw i32 %j, 4
%add = add nsw i32 %mul, %i
%A_idx = getelementptr inbounds double, ptr %A, i32 %add
store double 42.0, ptr %A_idx
br label %inner.inc
inner.inc:
%i.inc = add nuw nsw i32 %i, 1
%i.cmp = icmp slt i32 %i.inc, 4
br i1 %i.cmp, label %inner.for, label %inner.exit
inner.exit:
br label %outer.inc
outer.inc:
%j.inc = add nuw nsw i32 %j, 1
br label %outer.for
outer.exit:
br label %return
return:
ret void
}
; CHECK: Stmt_body
; CHECK: MustWriteAccess := [Reduction Type: NONE] [Scalar: 0]
; CHECK-NEXT: { Stmt_body[i0, i1] -> MemRef_A[4i0 + i1] };
; CHECK-NEXT: new: { Stmt_body[i0, i1] -> MemRef_A[i0, i1] };

View File

@ -0,0 +1,26 @@
{
"arrays": [
{
"name": "MemRef_A",
"sizes": [
"*"
],
"type": "double"
}
],
"context": "{ : }",
"name": "%outer.for---%return",
"statements": [
{
"accesses": [
{
"kind": "write",
"relation": "{ Stmt_body[i0, i1] -> MemRef_A[4i0 + i1] }"
}
],
"domain": "{ Stmt_body[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 3 }",
"name": "Stmt_body",
"schedule": "{ Stmt_body[i0, i1] -> [i0, i1] }"
}
]
}

View File

@ -0,0 +1,27 @@
{
"arrays": [
{
"name": "MemRef_A",
"sizes": [
"*",
"4"
],
"type": "double"
}
],
"context": "{ : }",
"name": "%outer.for---%return",
"statements": [
{
"accesses": [
{
"kind": "write",
"relation": "{ Stmt_body[i0, i1] -> MemRef_A[i0][i1] }"
}
],
"domain": "{ Stmt_body[i0, i1] : 0 <= i0 <= 1 and 0 <= i1 <= 3 }",
"name": "Stmt_body",
"schedule": "{ Stmt_body[i0, i1] -> [i0, i1] }"
}
]
}