Skip to content

Commit 663209b

Browse files
htyulanza
authored andcommitted
[ClR] Set optnone attribute for functions. (#115)
Summary: Setting the Optnone attribute for CIR functions and progating it all the way down to LLVM IR for those not supposed to be optimized.
1 parent 9155ecc commit 663209b

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ class CIR_Attr<string name, string attrMnemonic, list<Trait> traits = []>
2626
let mnemonic = attrMnemonic;
2727
}
2828

29+
class CIRUnitAttr<string name, string attrMnemonic, list<Trait> traits = []>
30+
: CIR_Attr<name, attrMnemonic, traits> {
31+
let returnType = "bool";
32+
let defaultValue = "false";
33+
let valueType = NoneType;
34+
let isOptional = 1;
35+
}
36+
2937
//===----------------------------------------------------------------------===//
3038
// LangAttr
3139
//===----------------------------------------------------------------------===//
@@ -440,4 +448,8 @@ def InlineAttr : CIR_Attr<"Inline", "inline"> {
440448
}];
441449
}
442450

451+
def OptNoneAttr : CIRUnitAttr<"OptNone", "optnone"> {
452+
let storageType = [{ OptNoneAttr }];
453+
}
454+
443455
#endif // MLIR_CIR_DIALECT_CIR_ATTRS

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,7 +1905,26 @@ void CIRGenModule::setExtraAttributesForFunc(FuncOp f,
19051905
mlir::cir::InlineKind::NoInline);
19061906
attrs.set(attr.getMnemonic(), attr);
19071907
}
1908+
}
1909+
1910+
// Track whether we need to add the optnone attribute,
1911+
// starting with the default for this optimization level.
1912+
bool ShouldAddOptNone =
1913+
!codeGenOpts.DisableO0ImplyOptNone && codeGenOpts.OptimizationLevel == 0;
1914+
if (FD) {
1915+
ShouldAddOptNone &= !FD->hasAttr<MinSizeAttr>();
1916+
ShouldAddOptNone &= !FD->hasAttr<AlwaysInlineAttr>();
1917+
ShouldAddOptNone |= FD->hasAttr<OptimizeNoneAttr>();
1918+
}
1919+
1920+
if (ShouldAddOptNone) {
1921+
auto optNoneAttr = mlir::cir::OptNoneAttr::get(builder.getContext());
1922+
attrs.set(optNoneAttr.getMnemonic(), optNoneAttr);
19081923

1924+
// OptimizeNone implies noinline; we should not be inlining such functions.
1925+
auto noInlineAttr = mlir::cir::InlineAttr::get(
1926+
builder.getContext(), mlir::cir::InlineKind::NoInline);
1927+
attrs.set(noInlineAttr.getMnemonic(), noInlineAttr);
19091928
}
19101929

19111930
f.setExtraAttrsAttr(mlir::cir::ExtraFuncAttributesAttr::get(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class CIRDialectLLVMIRTranslationInterface
6767
llvmFunc->addFnAttr(llvm::Attribute::InlineHint);
6868
else
6969
llvm_unreachable("Unknown inline kind");
70+
} else if (attr.getValue().dyn_cast<mlir::cir::OptNoneAttr>()) {
71+
llvmFunc->addFnAttr(llvm::Attribute::OptimizeNone);
7072
}
7173
}
7274
}

clang/test/CIR/CodeGen/optnone.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %clang_cc1 -O0 -triple x86_64-unknown-linux-gnu -fclangir-enable -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR-O0
3+
// RUN: %clang_cc1 -O0 -triple x86_64-unknown-linux-gnu -fclangir-enable -emit-llvm %s -o %t.ll
4+
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM-O0
5+
6+
// RUN: %clang_cc1 -O2 -triple x86_64-unknown-linux-gnu -fclangir-enable -emit-cir %s -o %t2.cir
7+
// RUN: FileCheck --input-file=%t2.cir %s -check-prefix=CIR-O2
8+
// RUN: %clang_cc1 -O2 -triple x86_64-unknown-linux-gnu -fclangir-enable -emit-llvm %s -o %t2.ll
9+
// RUN: FileCheck --input-file=%t2.ll %s -check-prefix=LLVM-O2
10+
11+
int s0(int a, int b) {
12+
int x = a + b;
13+
if (x > 0)
14+
x = 0;
15+
else
16+
x = 1;
17+
return x;
18+
}
19+
20+
// CIR-O0: cir.func @_Z2s0ii(%arg0:{{.*}}, %arg1:{{.*}} -> {{.*}} extra( {inline = #cir.inline<no>, optnone = #cir.optnone} )
21+
// CIR-O2-NOT: cir.func @_Z2s0ii(%arg0:{{.*}}, %arg1:{{.*}} -> {{.*}} optnone
22+
23+
// LLVM-O0: define i32 @_Z2s0ii(i32 %0, i32 %1) #[[#ATTR:]]
24+
// LLVM-O0: attributes #[[#ATTR]] = { noinline optnone }
25+
// LLVM-O2-NOT: attributes #[[#]] = { noinline optnone }

0 commit comments

Comments
 (0)