Skip to content

Commit 81aaca3

Browse files
authored
[CIR][NFC] Use TableGen to generate LLVM lowering patterns (llvm#159390)
Most lowering patterns have exactly the same class declaration with different names and different `matchAndRewrite` implementations, yet their declaration occupies near 1000 lines of code in `LowerToLLVM.h`, making this file difficult to read and boring to maintain. In this patch, I migrate their declarations to be generated from `CIROps.td` using `clang-tblgen`. Some extra `CIR_Op` TableGen fields are introduced to help this: - The `CIR_Op` class now defines a `bit` field `hasLLVMLowering` which defaults to `true`. If its value is `true`, `clang-tblgen` would generate an LLVM lowering pattern declaration for the operation. - Some LLVM lowering patterns has bounded recursion. This could be enabled by setting the `isLLVMLoweringRecursive` field in a `CIR_Op` record to `true`. - Some LLVM lowering patterns have defined additional class members. They could be listed in the `extraLLVMLoweringPatternDecl` field. Note that in the incubator we have a similar TableGen code generator that generates LLVM lowering code for CIR builtin ops which has a one-to-one correspondence to LLVM dialect operations. This patch does NOT try to upstream it. Some additional noticeable changes made by this patch: - This patch adds the `dataLayout` member to every LLVM lowering pattern class to make the job easier for a code generator. In the future we might want to add more members to the lowering patterns, and we will need to update the code generator to make such changes.
1 parent 29620d9 commit 81aaca3

File tree

8 files changed

+223
-876
lines changed

8 files changed

+223
-876
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,15 @@ class LLVMLoweringInfo {
7878
}
7979

8080
class CIR_Op<string mnemonic, list<Trait> traits = []> :
81-
Op<CIR_Dialect, mnemonic, traits>, LLVMLoweringInfo;
81+
Op<CIR_Dialect, mnemonic, traits>, LLVMLoweringInfo {
82+
// Should we generate an LLVM lowering pattern for this op?
83+
bit hasLLVMLowering = true;
84+
// Is the LLVM lowering pattern for this operation recursive?
85+
bit isLLVMLoweringRecursive = false;
86+
// Extra class declarations to be included in the generated LLVM lowering
87+
// pattern.
88+
code extraLLVMLoweringPatternDecl = "";
89+
}
8290

8391
//===----------------------------------------------------------------------===//
8492
// CastOp
@@ -218,6 +226,10 @@ def CIR_CastOp : CIR_Op<"cast", [
218226
// The input and output types should match the cast kind.
219227
let hasVerifier = 1;
220228
let hasFolder = 1;
229+
230+
let extraLLVMLoweringPatternDecl = [{
231+
mlir::Type convertTy(mlir::Type ty) const;
232+
}];
221233
}
222234

223235

@@ -297,6 +309,8 @@ def CIR_ConstantOp : CIR_Op<"const", [
297309
}];
298310

299311
let hasFolder = 1;
312+
313+
let isLLVMLoweringRecursive = true;
300314
}
301315

302316
//===----------------------------------------------------------------------===//
@@ -613,6 +627,8 @@ def CIR_IfOp : CIR_Op<"if", [
613627
CArg<"BuilderCallbackRef", "buildTerminatedBody">:$thenBuilder,
614628
CArg<"BuilderCallbackRef", "nullptr">:$elseBuilder)>
615629
];
630+
631+
let hasLLVMLowering = false;
616632
}
617633

618634
//===----------------------------------------------------------------------===//
@@ -659,6 +675,7 @@ def CIR_ConditionOp : CIR_Op<"condition", [
659675
let arguments = (ins CIR_BoolType:$condition);
660676
let assemblyFormat = " `(` $condition `)` attr-dict ";
661677
let hasVerifier = 1;
678+
let hasLLVMLowering = false;
662679
}
663680

664681
//===----------------------------------------------------------------------===//
@@ -726,6 +743,8 @@ def CIR_YieldOp : CIR_Op<"yield", [
726743
let builders = [
727744
OpBuilder<(ins), [{ /* nothing to do */ }]>,
728745
];
746+
747+
let hasLLVMLowering = false;
729748
}
730749

731750
//===----------------------------------------------------------------------===//
@@ -741,6 +760,7 @@ def CIR_BreakOp : CIR_Op<"break", [Terminator]> {
741760
}];
742761
let assemblyFormat = "attr-dict";
743762
let hasVerifier = 1;
763+
let hasLLVMLowering = false;
744764
}
745765

746766
//===----------------------------------------------------------------------===//
@@ -756,6 +776,7 @@ def CIR_ContinueOp : CIR_Op<"continue", [Terminator]> {
756776
}];
757777
let assemblyFormat = "attr-dict";
758778
let hasVerifier = 1;
779+
let hasLLVMLowering = false;
759780
}
760781

761782
//===----------------------------------------------------------------------===//
@@ -814,6 +835,8 @@ def CIR_ScopeOp : CIR_Op<"scope", [
814835
// Scopes without yielding values.
815836
OpBuilder<(ins "llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)>":$scopeBuilder)>
816837
];
838+
839+
let hasLLVMLowering = false;
817840
}
818841

819842
//===----------------------------------------------------------------------===//
@@ -860,6 +883,8 @@ def CIR_CaseOp : CIR_Op<"case", [
860883
"CaseOpKind":$kind,
861884
"mlir::OpBuilder::InsertPoint &":$insertPoint)>
862885
];
886+
887+
let hasLLVMLowering = false;
863888
}
864889

865890
def CIR_SwitchOp : CIR_Op<"switch", [
@@ -1025,6 +1050,8 @@ def CIR_SwitchOp : CIR_Op<"switch", [
10251050
// This is an expensive and need to be used with caution.
10261051
bool isSimpleForm(llvm::SmallVectorImpl<CaseOp> &cases);
10271052
}];
1053+
1054+
let hasLLVMLowering = false;
10281055
}
10291056

10301057
//===----------------------------------------------------------------------===//
@@ -1170,6 +1197,8 @@ def CIR_GotoOp : CIR_Op<"goto", [Terminator]> {
11701197
}];
11711198
let arguments = (ins StrAttr:$label);
11721199
let assemblyFormat = [{ $label attr-dict }];
1200+
1201+
let hasLLVMLowering = false;
11731202
}
11741203

11751204
//===----------------------------------------------------------------------===//
@@ -1185,6 +1214,8 @@ def CIR_LabelOp : CIR_Op<"label", [AlwaysSpeculatable]> {
11851214
let arguments = (ins StrAttr:$label);
11861215
let assemblyFormat = [{ $label attr-dict }];
11871216
let hasVerifier = 1;
1217+
1218+
let hasLLVMLowering = false;
11881219
}
11891220

11901221
//===----------------------------------------------------------------------===//
@@ -1349,6 +1380,8 @@ def CIR_WhileOp : CIR_WhileOpBase<"while"> {
13491380
}
13501381
```
13511382
}];
1383+
1384+
let hasLLVMLowering = false;
13521385
}
13531386

13541387
def CIR_DoWhileOp : CIR_WhileOpBase<"do"> {
@@ -1375,6 +1408,8 @@ def CIR_DoWhileOp : CIR_WhileOpBase<"do"> {
13751408
}
13761409
```
13771410
}];
1411+
1412+
let hasLLVMLowering = false;
13781413
}
13791414

13801415
//===----------------------------------------------------------------------===//
@@ -1442,6 +1477,8 @@ def CIR_ForOp : CIR_LoopOpBase<"for"> {
14421477
return llvm::SmallVector<mlir::Region *, 3>{&getCond(), &getBody(), &getStep()};
14431478
}
14441479
}];
1480+
1481+
let hasLLVMLowering = false;
14451482
}
14461483

14471484
//===----------------------------------------------------------------------===//
@@ -1480,6 +1517,8 @@ def CIR_CmpOp : CIR_Op<"cmp", [Pure, SameTypeOperands]> {
14801517
let assemblyFormat = [{
14811518
`(` $kind `,` $lhs `,` $rhs `)` `:` type($lhs) `,` type($result) attr-dict
14821519
}];
1520+
1521+
let isLLVMLoweringRecursive = true;
14831522
}
14841523

14851524
//===----------------------------------------------------------------------===//
@@ -1550,6 +1589,10 @@ def CIR_BinOp : CIR_Op<"binop", [
15501589
}];
15511590

15521591
let hasVerifier = 1;
1592+
1593+
let extraLLVMLoweringPatternDecl = [{
1594+
mlir::LLVM::IntegerOverflowFlags getIntOverflowFlag(cir::BinOp op) const;
1595+
}];
15531596
}
15541597

15551598
//===----------------------------------------------------------------------===//
@@ -1687,6 +1730,8 @@ def CIR_TernaryOp : CIR_Op<"ternary", [
16871730
`false` $falseRegion
16881731
`)` `:` functional-type(operands, results) attr-dict
16891732
}];
1733+
1734+
let hasLLVMLowering = false;
16901735
}
16911736

16921737
//===----------------------------------------------------------------------===//
@@ -1790,6 +1835,20 @@ def CIR_GlobalOp : CIR_Op<"global", [
17901835
"cir::GlobalLinkageKind::ExternalLinkage">:$linkage)>];
17911836

17921837
let hasVerifier = 1;
1838+
1839+
let isLLVMLoweringRecursive = true;
1840+
let extraLLVMLoweringPatternDecl = [{
1841+
mlir::LogicalResult matchAndRewriteRegionInitializedGlobal(
1842+
cir::GlobalOp op, mlir::Attribute init,
1843+
mlir::ConversionPatternRewriter &rewriter) const;
1844+
1845+
void setupRegionInitializedLLVMGlobalOp(
1846+
cir::GlobalOp op, mlir::ConversionPatternRewriter &rewriter) const;
1847+
1848+
mutable mlir::LLVM::ComdatOp comdatOp = nullptr;
1849+
mlir::SymbolRefAttr getComdatAttr(cir::GlobalOp &op,
1850+
mlir::OpBuilder &builder) const;
1851+
}];
17931852
}
17941853

17951854
//===----------------------------------------------------------------------===//
@@ -2340,6 +2399,19 @@ def CIR_FuncOp : CIR_Op<"func", [
23402399

23412400
let hasCustomAssemblyFormat = 1;
23422401
let hasVerifier = 1;
2402+
2403+
let extraLLVMLoweringPatternDecl = [{
2404+
static mlir::StringRef getLinkageAttrNameString() { return "linkage"; }
2405+
2406+
void lowerFuncAttributes(
2407+
cir::FuncOp func, bool filterArgAndResAttrs,
2408+
mlir::SmallVectorImpl<mlir::NamedAttribute> &result) const;
2409+
2410+
mlir::LogicalResult
2411+
matchAndRewriteAlias(cir::FuncOp op, llvm::StringRef aliasee, mlir::Type ty,
2412+
OpAdaptor adaptor,
2413+
mlir::ConversionPatternRewriter &rewriter) const;
2414+
}];
23432415
}
23442416

23452417
//===----------------------------------------------------------------------===//
@@ -2761,6 +2833,8 @@ class CIR_ArrayInitDestroy<string mnemonic> : CIR_Op<mnemonic> {
27612833
regionBuilder($_builder, $_state.location);
27622834
}]>
27632835
];
2836+
2837+
let hasLLVMLowering = false;
27642838
}
27652839

27662840
def CIR_ArrayCtor : CIR_ArrayInitDestroy<"array.ctor"> {
@@ -3380,6 +3454,8 @@ def CIR_ComplexMulOp : CIR_Op<"complex.mul", [
33803454
let assemblyFormat = [{
33813455
$lhs `,` $rhs `range` `(` $range `)` `:` qualified(type($result)) attr-dict
33823456
}];
3457+
3458+
let hasLLVMLowering = false;
33833459
}
33843460

33853461
def CIR_ComplexDivOp : CIR_Op<"complex.div", [
@@ -3422,6 +3498,8 @@ def CIR_ComplexDivOp : CIR_Op<"complex.div", [
34223498
let assemblyFormat = [{
34233499
$lhs `,` $rhs `range` `(` $range `)` `:` qualified(type($result)) attr-dict
34243500
}];
3501+
3502+
let hasLLVMLowering = false;
34253503
}
34263504

34273505
//===----------------------------------------------------------------------===//

clang/include/clang/CIR/Dialect/IR/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ mlir_tablegen(CIROpsAttributes.h.inc -gen-attrdef-decls)
2020
mlir_tablegen(CIROpsAttributes.cpp.inc -gen-attrdef-defs)
2121
add_public_tablegen_target(MLIRCIREnumsGen)
2222

23+
clang_tablegen(CIRLowering.inc -gen-cir-lowering
24+
SOURCE CIROps.td
25+
TARGET CIRLowering)
26+
2327
set(LLVM_TARGET_DEFINITIONS CIRTypeConstraints.td)
2428
mlir_tablegen(CIRTypeConstraints.h.inc -gen-type-constraint-decls)
2529
mlir_tablegen(CIRTypeConstraints.cpp.inc -gen-type-constraint-defs)

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 4 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2463,87 +2463,11 @@ void ConvertCIRToLLVMPass::runOnOperation() {
24632463

24642464
mlir::RewritePatternSet patterns(&getContext());
24652465

2466-
patterns.add<CIRToLLVMReturnOpLowering>(patterns.getContext());
2467-
// This could currently be merged with the group below, but it will get more
2468-
// arguments later, so we'll keep it separate for now.
2469-
patterns.add<CIRToLLVMAllocaOpLowering>(converter, patterns.getContext(), dl);
2470-
patterns.add<CIRToLLVMLoadOpLowering>(converter, patterns.getContext(), dl);
2471-
patterns.add<CIRToLLVMStoreOpLowering>(converter, patterns.getContext(), dl);
2472-
patterns.add<CIRToLLVMGlobalOpLowering>(converter, patterns.getContext(), dl);
2473-
patterns.add<CIRToLLVMCastOpLowering>(converter, patterns.getContext(), dl);
2474-
patterns.add<CIRToLLVMPtrStrideOpLowering>(converter, patterns.getContext(),
2475-
dl);
2476-
patterns.add<CIRToLLVMInlineAsmOpLowering>(converter, patterns.getContext(),
2477-
dl);
24782466
patterns.add<
2479-
// clang-format off
2480-
CIRToLLVMACosOpLowering,
2481-
CIRToLLVMASinOpLowering,
2482-
CIRToLLVMAssumeOpLowering,
2483-
CIRToLLVMAssumeAlignedOpLowering,
2484-
CIRToLLVMAssumeSepStorageOpLowering,
2485-
CIRToLLVMAtomicCmpXchgLowering,
2486-
CIRToLLVMBaseClassAddrOpLowering,
2487-
CIRToLLVMATanOpLowering,
2488-
CIRToLLVMBinOpLowering,
2489-
CIRToLLVMBitClrsbOpLowering,
2490-
CIRToLLVMBitClzOpLowering,
2491-
CIRToLLVMBitCtzOpLowering,
2492-
CIRToLLVMBitFfsOpLowering,
2493-
CIRToLLVMBitParityOpLowering,
2494-
CIRToLLVMBitPopcountOpLowering,
2495-
CIRToLLVMBitReverseOpLowering,
2496-
CIRToLLVMBrCondOpLowering,
2497-
CIRToLLVMBrOpLowering,
2498-
CIRToLLVMByteSwapOpLowering,
2499-
CIRToLLVMCallOpLowering,
2500-
CIRToLLVMCmpOpLowering,
2501-
CIRToLLVMComplexAddOpLowering,
2502-
CIRToLLVMComplexCreateOpLowering,
2503-
CIRToLLVMComplexImagOpLowering,
2504-
CIRToLLVMComplexImagPtrOpLowering,
2505-
CIRToLLVMComplexRealOpLowering,
2506-
CIRToLLVMComplexRealPtrOpLowering,
2507-
CIRToLLVMComplexSubOpLowering,
2508-
CIRToLLVMCopyOpLowering,
2509-
CIRToLLVMCosOpLowering,
2510-
CIRToLLVMConstantOpLowering,
2511-
CIRToLLVMExpectOpLowering,
2512-
CIRToLLVMFAbsOpLowering,
2513-
CIRToLLVMFrameAddrOpLowering,
2514-
CIRToLLVMFuncOpLowering,
2515-
CIRToLLVMGetBitfieldOpLowering,
2516-
CIRToLLVMGetGlobalOpLowering,
2517-
CIRToLLVMGetMemberOpLowering,
2518-
CIRToLLVMReturnAddrOpLowering,
2519-
CIRToLLVMRotateOpLowering,
2520-
CIRToLLVMSelectOpLowering,
2521-
CIRToLLVMSetBitfieldOpLowering,
2522-
CIRToLLVMShiftOpLowering,
2523-
CIRToLLVMStackRestoreOpLowering,
2524-
CIRToLLVMStackSaveOpLowering,
2525-
CIRToLLVMSwitchFlatOpLowering,
2526-
CIRToLLVMThrowOpLowering,
2527-
CIRToLLVMTrapOpLowering,
2528-
CIRToLLVMUnaryOpLowering,
2529-
CIRToLLVMUnreachableOpLowering,
2530-
CIRToLLVMVAArgOpLowering,
2531-
CIRToLLVMVAEndOpLowering,
2532-
CIRToLLVMVAStartOpLowering,
2533-
CIRToLLVMVecCmpOpLowering,
2534-
CIRToLLVMVecCreateOpLowering,
2535-
CIRToLLVMVecExtractOpLowering,
2536-
CIRToLLVMVecInsertOpLowering,
2537-
CIRToLLVMVecShuffleDynamicOpLowering,
2538-
CIRToLLVMVecShuffleOpLowering,
2539-
CIRToLLVMVecSplatOpLowering,
2540-
CIRToLLVMVecTernaryOpLowering,
2541-
CIRToLLVMVTableAddrPointOpLowering,
2542-
CIRToLLVMVTableGetVPtrOpLowering,
2543-
CIRToLLVMVTableGetVirtualFnAddrOpLowering,
2544-
CIRToLLVMVTTAddrPointOpLowering
2545-
// clang-format on
2546-
>(converter, patterns.getContext());
2467+
#define GET_LLVM_LOWERING_PATTERNS_LIST
2468+
#include "clang/CIR/Dialect/IR/CIRLowering.inc"
2469+
#undef GET_LLVM_LOWERING_PATTERNS_LIST
2470+
>(converter, patterns.getContext(), dl);
25472471

25482472
processCIRAttrs(module);
25492473

0 commit comments

Comments
 (0)