Skip to content

Commit e640960

Browse files
georgestagglionel-
andcommitted
Rewrite index types according to target
Co-authored-by: Lionel Henry <[email protected]>
1 parent 8634ea5 commit e640960

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

flang/include/flang/Optimizer/CodeGen/Target.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ class CodeGenSpecifics {
8787
virtual Marshalling complexReturnType(mlir::Location loc,
8888
mlir::Type eleTy) const = 0;
8989

90+
// Type presentation of a `index` type value in memory.
91+
virtual mlir::Type indexMemoryType(mlir::Type eleTy) const = 0;
92+
9093
/// Type presentation of a `boxchar<n>` type value in memory.
9194
virtual mlir::Type boxcharMemoryType(mlir::Type eleTy) const = 0;
9295

flang/lib/Optimizer/CodeGen/Target.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ struct GenericTarget : public CodeGenSpecifics {
5858
mlir::TypeRange{eleTy, eleTy});
5959
}
6060

61+
mlir::Type indexMemoryType(mlir::Type eleTy) const override {
62+
return mlir::IntegerType::get(eleTy.getContext(), S::defaultWidth);
63+
}
64+
6165
mlir::Type boxcharMemoryType(mlir::Type eleTy) const override {
6266
auto idxTy = mlir::IntegerType::get(eleTy.getContext(), S::defaultWidth);
6367
auto ptrTy = fir::ReferenceType::get(eleTy);

flang/lib/Optimizer/CodeGen/TargetRewrite.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,12 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
272272
mlir::Value oper = std::get<1>(e.value());
273273
unsigned index = e.index();
274274
llvm::TypeSwitch<mlir::Type>(ty)
275+
.template Case<mlir::IndexType>([&](mlir::IndexType indexTy) {
276+
auto newTy = specifics->indexMemoryType(indexTy);
277+
auto newOper = rewriter->create<mlir::arith::IndexCastOp>(loc, newTy, oper);
278+
newInTys.push_back(newTy);
279+
newOpers.push_back(newOper);
280+
})
275281
.template Case<fir::BoxCharType>([&](fir::BoxCharType boxTy) {
276282
bool sret;
277283
if constexpr (std::is_same_v<std::decay_t<A>, fir::CallOp>) {
@@ -534,6 +540,7 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
534540
for (auto ty : func.getResults())
535541
if ((ty.isa<fir::BoxCharType>() && !noCharacterConversion) ||
536542
(fir::isa_complex(ty) && !noComplexConversion) ||
543+
(ty.isa<mlir::IndexType>()) ||
537544
(ty.isa<mlir::IntegerType>() && hasCCallingConv)) {
538545
LLVM_DEBUG(llvm::dbgs() << "rewrite " << signature << " for target\n");
539546
return false;
@@ -542,6 +549,7 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
542549
if (((ty.isa<fir::BoxCharType>() || fir::isCharacterProcedureTuple(ty)) &&
543550
!noCharacterConversion) ||
544551
(fir::isa_complex(ty) && !noComplexConversion) ||
552+
(ty.isa<mlir::IndexType>()) ||
545553
(ty.isa<mlir::IntegerType>() && hasCCallingConv)) {
546554
LLVM_DEBUG(llvm::dbgs() << "rewrite " << signature << " for target\n");
547555
return false;
@@ -624,6 +632,9 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
624632
auto ty = e.value();
625633
unsigned index = e.index();
626634
llvm::TypeSwitch<mlir::Type>(ty)
635+
.Case<mlir::IndexType>([&](mlir::IndexType indexTy) {
636+
newInTys.push_back(specifics->indexMemoryType(indexTy));
637+
})
627638
.Case<fir::BoxCharType>([&](fir::BoxCharType boxTy) {
628639
if (noCharacterConversion) {
629640
newInTys.push_back(boxTy);

0 commit comments

Comments
 (0)