Skip to content

Commit c6ce4ed

Browse files
committed
[rebase] reapply lost fir-dev changes from 68fe9e4
Strangely, it seems codegen change from 68fe9e4 were just lost... reapply them.
1 parent 7aec375 commit c6ce4ed

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

flang/lib/Optimizer/CodeGen/CodeGen.cpp

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,16 +1002,15 @@ struct EmboxCommonConversion : public FIROpConversion<OP> {
10021002
// The ancestor OpenMP Op which is outlineable.
10031003
// The ancestor LLVM Function Op.
10041004
if (auto iface =
1005-
thisBlock->getParent()
1006-
->getParentOfType<
1007-
mlir::omp::OutlineableOpenMPOpInterface>()) {
1008-
rewriter.setInsertionPointToStart(iface.getAllocaBlock());
1009-
} else {
1010-
auto func = mlir::isa<mlir::LLVM::LLVMFuncOp>(op) ?
1011-
mlir::cast<mlir::LLVM::LLVMFuncOp>(op) :
1012-
op->getParentOfType<mlir::LLVM::LLVMFuncOp>();
1013-
rewriter.setInsertionPointToStart(&func.front());
1014-
}
1005+
thisBlock->getParent()
1006+
->getParentOfType<mlir::omp::OutlineableOpenMPOpInterface>()) {
1007+
rewriter.setInsertionPointToStart(iface.getAllocaBlock());
1008+
} else {
1009+
auto func = mlir::isa<mlir::LLVM::LLVMFuncOp>(op)
1010+
? mlir::cast<mlir::LLVM::LLVMFuncOp>(op)
1011+
: op->getParentOfType<mlir::LLVM::LLVMFuncOp>();
1012+
rewriter.setInsertionPointToStart(&func.front());
1013+
}
10151014
auto sz = this->genConstantOffset(loc, rewriter, 1);
10161015
auto al = rewriter.create<mlir::LLVM::AllocaOp>(loc, toTy, sz, alignment);
10171016
rewriter.restoreInsertionPoint(thisPt);
@@ -1111,13 +1110,12 @@ struct EmboxCommonConversion : public FIROpConversion<OP> {
11111110
return doCharacter(charWidth, len);
11121111
}
11131112
assert(!lenParams.empty());
1114-
return doCharacter(charWidth, lenParams[0]);
1113+
return doCharacter(charWidth, lenParams.back());
11151114
}
11161115
if (auto ty = boxEleTy.dyn_cast<fir::LogicalType>())
11171116
return doLogical(getKindMap().getLogicalBitsize(ty.getFKind()));
1118-
if (auto seqTy = boxEleTy.dyn_cast<fir::SequenceType>()) {
1117+
if (auto seqTy = boxEleTy.dyn_cast<fir::SequenceType>())
11191118
return getSizeAndTypeCode(loc, rewriter, seqTy.getEleTy(), lenParams);
1120-
}
11211119
if (boxEleTy.isa<fir::RecordType>()) {
11221120
auto ptrTy = mlir::LLVM::LLVMPointerType::get(
11231121
this->lowerTy().convertType(boxEleTy));
@@ -1228,9 +1226,15 @@ struct EmboxCommonConversion : public FIROpConversion<OP> {
12281226
auto llvmBoxTy = llvmBoxPtrTy.getElementType();
12291227
mlir::Value dest = rewriter.create<mlir::LLVM::UndefOp>(loc, llvmBoxTy);
12301228

1229+
llvm::SmallVector<mlir::Value> typeparams = lenParams;
1230+
if constexpr (!std::is_same_v<BOX, fir::EmboxOp>) {
1231+
if (!box.substr().empty() && fir::hasDynamicSize(boxTy.getEleTy()))
1232+
typeparams.push_back(box.substr()[1]);
1233+
}
1234+
12311235
// Write each of the fields with the appropriate values
12321236
auto [eleSize, cfiTy] =
1233-
getSizeAndTypeCode(loc, rewriter, boxTy.getEleTy(), lenParams);
1237+
getSizeAndTypeCode(loc, rewriter, boxTy.getEleTy(), typeparams);
12341238
dest = insertField(rewriter, loc, dest, {1}, eleSize);
12351239
dest = insertField(rewriter, loc, dest, {2},
12361240
this->genConstantOffset(loc, rewriter, CFI_VERSION));
@@ -1447,7 +1451,7 @@ struct XEmboxOpConversion : public EmboxCommonConversion<fir::cg::XEmboxOp> {
14471451
if (hasSlice)
14481452
sliceOff += 3;
14491453
}
1450-
if (hasSlice || hasSubcomp) {
1454+
if (hasSlice || hasSubcomp || !xbox.substr().empty()) {
14511455
llvm::SmallVector<mlir::Value> args = {base, ptrOffset};
14521456
args.append(gepArgs.rbegin(), gepArgs.rend());
14531457
if (hasSubcomp) {
@@ -1461,6 +1465,10 @@ struct XEmboxOpConversion : public EmboxCommonConversion<fir::cg::XEmboxOp> {
14611465
const auto *lastOffset = beginOffset + xbox.subcomponent().size();
14621466
args.append(beginOffset, lastOffset);
14631467
}
1468+
if (!xbox.substr().empty()) {
1469+
// Append the substring starting offset.
1470+
args.push_back(xbox.substr()[0]);
1471+
}
14641472
base = rewriter.create<mlir::LLVM::GEPOp>(loc, base.getType(), args);
14651473
}
14661474
dest = insertBaseAddress(rewriter, loc, dest, base);
@@ -2458,7 +2466,7 @@ struct GlobalOpConversion : public FIROpConversion<fir::GlobalOp> {
24582466
auto &gr = g.getInitializerRegion();
24592467
rewriter.inlineRegionBefore(global.region(), gr, gr.end());
24602468
if (!gr.empty()) {
2461-
// Replace insert_on_range with a constant dense attribute if the
2469+
// Replace insert_on_range with a constant dense attribute if the
24622470
// initialization is on the full range.
24632471
auto insertOnRangeOps = gr.front().getOps<fir::InsertOnRangeOp>();
24642472
for (auto insertOp : insertOnRangeOps) {
@@ -2467,11 +2475,11 @@ struct GlobalOpConversion : public FIROpConversion<fir::GlobalOp> {
24672475
auto *op = insertOp.val().getDefiningOp();
24682476
auto constant = mlir::dyn_cast<mlir::arith::ConstantOp>(op);
24692477
if (!constant) {
2470-
auto convertOp = mlir::dyn_cast<fir::ConvertOp>(op);
2471-
if (!convertOp)
2472-
continue;
2473-
constant =
2474-
cast<mlir::arith::ConstantOp>(convertOp.value().getDefiningOp());
2478+
auto convertOp = mlir::dyn_cast<fir::ConvertOp>(op);
2479+
if (!convertOp)
2480+
continue;
2481+
constant =
2482+
cast<mlir::arith::ConstantOp>(convertOp.value().getDefiningOp());
24752483
}
24762484
mlir::Type vecType = mlir::VectorType::get(
24772485
insertOp.getType().getShape(), constant.getType());
@@ -2491,10 +2499,10 @@ struct GlobalOpConversion : public FIROpConversion<fir::GlobalOp> {
24912499
auto extents = seqTy.getShape();
24922500
if (indexes.size() / 2 != extents.size())
24932501
return false;
2494-
for (unsigned i = 0; i < indexes.size(); i+= 2) {
2502+
for (unsigned i = 0; i < indexes.size(); i += 2) {
24952503
if (indexes[i].cast<IntegerAttr>().getInt() != 0)
24962504
return false;
2497-
if (indexes[i+1].cast<IntegerAttr>().getInt() != extents[i/2] - 1)
2505+
if (indexes[i + 1].cast<IntegerAttr>().getInt() != extents[i / 2] - 1)
24982506
return false;
24992507
}
25002508
return true;

0 commit comments

Comments
 (0)