Skip to content

Commit b46b3ba

Browse files
Kureelanza
authored andcommitted
[CIR][Lowering] Add scf.scope lowering to standard dialects (llvm#262)
This PR adds MLIR lowering of `cir.scope`. I also notice that the MLIR unit tests still uses old integer types. I will fix those in a separate PR.
1 parent 60e8fa8 commit b46b3ba

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,12 +501,43 @@ class CIRBrOpLowering : public mlir::OpRewritePattern<mlir::cir::BrOp> {
501501
}
502502
};
503503

504+
class CIRScopeOpLowering : public mlir::OpRewritePattern<mlir::cir::ScopeOp> {
505+
using mlir::OpRewritePattern<mlir::cir::ScopeOp>::OpRewritePattern;
506+
507+
mlir::LogicalResult
508+
matchAndRewrite(mlir::cir::ScopeOp scopeOp,
509+
mlir::PatternRewriter &rewriter) const override {
510+
// Empty scope: just remove it.
511+
if (scopeOp.getRegion().empty()) {
512+
rewriter.eraseOp(scopeOp);
513+
return mlir::success();
514+
}
515+
516+
for (auto &block : scopeOp.getRegion()) {
517+
rewriter.setInsertionPointToEnd(&block);
518+
auto *terminator = block.getTerminator();
519+
rewriter.replaceOpWithNewOp<mlir::memref::AllocaScopeReturnOp>(
520+
terminator, terminator->getOperands());
521+
}
522+
523+
rewriter.setInsertionPoint(scopeOp);
524+
auto newScopeOp = rewriter.create<mlir::memref::AllocaScopeOp>(
525+
scopeOp.getLoc(), scopeOp.getResultTypes());
526+
rewriter.inlineRegionBefore(scopeOp.getScopeRegion(),
527+
newScopeOp.getBodyRegion(),
528+
newScopeOp.getBodyRegion().end());
529+
rewriter.replaceOp(scopeOp, newScopeOp);
530+
531+
return mlir::LogicalResult::success();
532+
}
533+
};
534+
504535
void populateCIRToMLIRConversionPatterns(mlir::RewritePatternSet &patterns,
505536
mlir::TypeConverter &converter) {
506537
patterns.add<CIRAllocaLowering, CIRLoadLowering, CIRStoreLowering,
507538
CIRConstantLowering, CIRUnaryOpLowering, CIRBinOpLowering,
508539
CIRCmpOpLowering, CIRBrOpLowering, CIRCallLowering,
509-
CIRReturnLowering>(patterns.getContext());
540+
CIRReturnLowering, CIRScopeOpLowering>(patterns.getContext());
510541
patterns.add<CIRFuncLowering>(converter, patterns.getContext());
511542
}
512543

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// RUN: cir-opt %s -cir-to-mlir -o - | FileCheck %s -check-prefix=MLIR
2+
// RUN: cir-opt %s -cir-to-mlir -cir-mlir-to-llvm -o - | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM
3+
4+
module {
5+
cir.func @foo() {
6+
cir.scope {
7+
%0 = cir.alloca i32, cir.ptr <i32>, ["a", init] {alignment = 4 : i64}
8+
%1 = cir.const(4 : i32) : i32
9+
cir.store %1, %0 : i32, cir.ptr <i32>
10+
}
11+
cir.return
12+
}
13+
14+
// MLIR: func.func @foo()
15+
// MLIR-NEXT: memref.alloca_scope
16+
// MLIR-NEXT: %alloca = memref.alloca() {alignment = 4 : i64} : memref<i32>
17+
// MLIR-NEXT: %c4_i32 = arith.constant 4 : i32
18+
// MLIR-NEXT: memref.store %c4_i32, %alloca[] : memref<i32>
19+
// MLIR-NEXT: }
20+
// MLIR-NEXT: return
21+
22+
23+
// LLVM: define void @foo()
24+
// LLVM-NEXT: %1 = call ptr @llvm.stacksave.p0()
25+
// LLVM-NEXT: br label %2
26+
// LLVM-EMPTY:
27+
// LLVM-NEXT: 2:
28+
// LLVM-NEXT: %3 = alloca i32, i64 1, align 4
29+
// LLVM-NEXT: %4 = insertvalue { ptr, ptr, i64 } undef, ptr %3, 0
30+
// LLVM-NEXT: %5 = insertvalue { ptr, ptr, i64 } %4, ptr %3, 1
31+
// LLVM-NEXT: %6 = insertvalue { ptr, ptr, i64 } %5, i64 0, 2
32+
// LLVM-NEXT: %7 = extractvalue { ptr, ptr, i64 } %6, 1
33+
// LLVM-NEXT: store i32 4, ptr %7, align 4
34+
// LLVM-NEXT: call void @llvm.stackrestore.p0(ptr %1)
35+
// LLVM-NEXT: br label %8
36+
// LLVM-EMPTY:
37+
// LLVM-NEXT: 8:
38+
// LLVM-NEXT: ret void
39+
// LLVM-NEXT: }
40+
41+
42+
// Should drop empty scopes.
43+
cir.func @empty_scope() {
44+
cir.scope {
45+
}
46+
cir.return
47+
}
48+
// MLIR: func.func @empty_scope()
49+
// MLIR-NEXT: return
50+
// MLIR-NEXT: }
51+
52+
}

0 commit comments

Comments
 (0)