Skip to content

Commit 1841b02

Browse files
authored
[CIR] Add OptInfo attribute (llvm#146261)
This patch adds the `#cir.opt_info` attribute which holds module-level optimization information.
1 parent ce99db9 commit 1841b02

File tree

6 files changed

+105
-0
lines changed

6 files changed

+105
-0
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,54 @@ class CIRUnitAttr<string name, string attrMnemonic, list<Trait> traits = []>
6565
let isOptional = 1;
6666
}
6767

68+
//===----------------------------------------------------------------------===//
69+
// OptInfoAttr
70+
//===----------------------------------------------------------------------===//
71+
72+
def CIR_OptInfoAttr : CIR_Attr<"OptInfo", "opt_info"> {
73+
let summary =
74+
"A module-level attribute that holds the optimization information";
75+
let description = [{
76+
The `#cir.opt_info` attribute holds optimization related information. For
77+
now this attribute is a module-level attribute that gets attached to the
78+
module operation during CIRGen.
79+
80+
The `level` parameter gives the optimization level. It must be an integer
81+
between 0 and 3, inclusive. It corresponds to the `OptimizationLevel` field
82+
within the `clang::CodeGenOptions` structure.
83+
84+
The `size` parameter gives the code size optimization level. It must be an
85+
integer between 0 and 2, inclusive. It corresponds to the `OptimizeSize`
86+
field within the `clang::CodeGenOptions` structure.
87+
88+
The `level` and `size` parameters correspond to the optimization level
89+
command line options passed to clang driver. The table below lists the
90+
current correspondance relationship:
91+
92+
| Flag | `level` | `size` |
93+
|------------------|---------|--------|
94+
| `-O0` or nothing | 0 | 0 |
95+
| `-O1` | 1 | 0 |
96+
| `-O2` | 2 | 0 |
97+
| `-O3` | 3 | 0 |
98+
| `-Os` | 2 | 1 |
99+
| `-Oz` | 2 | 2 |
100+
101+
Examples:
102+
103+
```mlir
104+
#cir.opt_info<level = 2, size = 0> // -O2
105+
```
106+
}];
107+
108+
let parameters = (ins "unsigned":$level, "unsigned":$size);
109+
110+
let assemblyFormat = [{
111+
`<` struct(params) `>`
112+
}];
113+
let genVerifyDecl = 1;
114+
}
115+
68116
//===----------------------------------------------------------------------===//
69117
// BoolAttr
70118
//===----------------------------------------------------------------------===//

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def CIR_Dialect : Dialect {
3636

3737
let extraClassDeclaration = [{
3838
static llvm::StringRef getTripleAttrName() { return "cir.triple"; }
39+
static llvm::StringRef getOptInfoAttrName() { return "cir.opt_info"; }
3940

4041
void registerAttributes();
4142
void registerTypes();

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
104104

105105
theModule->setAttr(cir::CIRDialect::getTripleAttrName(),
106106
builder.getStringAttr(getTriple().str()));
107+
108+
if (cgo.OptimizationLevel > 0 || cgo.OptimizeSize > 0)
109+
theModule->setAttr(cir::CIRDialect::getOptInfoAttrName(),
110+
cir::OptInfoAttr::get(&mlirContext,
111+
cgo.OptimizationLevel,
112+
cgo.OptimizeSize));
107113
}
108114

109115
CIRGenModule::~CIRGenModule() = default;

clang/lib/CIR/Dialect/IR/CIRAttrs.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ void CIRDialect::printAttribute(Attribute attr, DialectAsmPrinter &os) const {
6868
llvm_unreachable("unexpected CIR type kind");
6969
}
7070

71+
//===----------------------------------------------------------------------===//
72+
// OptInfoAttr definitions
73+
//===----------------------------------------------------------------------===//
74+
75+
LogicalResult OptInfoAttr::verify(function_ref<InFlightDiagnostic()> emitError,
76+
unsigned level, unsigned size) {
77+
if (level > 3)
78+
return emitError()
79+
<< "optimization level must be between 0 and 3 inclusive";
80+
if (size > 2)
81+
return emitError()
82+
<< "size optimization level must be between 0 and 2 inclusive";
83+
return success();
84+
}
85+
7186
//===----------------------------------------------------------------------===//
7287
// ConstPtrAttr definitions
7388
//===----------------------------------------------------------------------===//
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CHECK-O0
3+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -O1 -fclangir -emit-cir %s -o %t.cir
4+
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CHECK-O1
5+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -O2 -fclangir -emit-cir %s -o %t.cir
6+
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CHECK-O2
7+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -O3 -fclangir -emit-cir %s -o %t.cir
8+
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CHECK-O3
9+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Os -fclangir -emit-cir %s -o %t.cir
10+
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CHECK-Os
11+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Oz -fclangir -emit-cir %s -o %t.cir
12+
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CHECK-Oz
13+
14+
void f() {}
15+
16+
// CHECK-O0: module attributes
17+
// CHECK-O0-NOT: cir.opt_info
18+
// CHECK-O1: module attributes {{.+}}cir.opt_info = #cir.opt_info<level = 1, size = 0>{{.+}}
19+
// CHECK-O2: module attributes {{.+}}cir.opt_info = #cir.opt_info<level = 2, size = 0>{{.+}}
20+
// CHECK-O3: module attributes {{.+}}cir.opt_info = #cir.opt_info<level = 3, size = 0>{{.+}}
21+
// CHECK-Os: module attributes {{.+}}cir.opt_info = #cir.opt_info<level = 2, size = 1>{{.+}}
22+
// CHECK-Oz: module attributes {{.+}}cir.opt_info = #cir.opt_info<level = 2, size = 2>{{.+}}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: cir-opt %s -verify-diagnostics -split-input-file
2+
3+
module attributes {
4+
// expected-error @below {{optimization level must be between 0 and 3 inclusive}}
5+
cir.opt_info = #cir.opt_info<level = 4, size = 0>
6+
} {}
7+
8+
// -----
9+
10+
module attributes {
11+
// expected-error @below {{size optimization level must be between 0 and 2 inclusive}}
12+
cir.opt_info = #cir.opt_info<level = 0, size = 3>
13+
} {}

0 commit comments

Comments
 (0)