Skip to content

[CIR] Add OptInfo attribute #146261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,54 @@ class CIRUnitAttr<string name, string attrMnemonic, list<Trait> traits = []>
let isOptional = 1;
}

//===----------------------------------------------------------------------===//
// OptInfoAttr
//===----------------------------------------------------------------------===//

def CIR_OptInfoAttr : CIR_Attr<"OptInfo", "opt_info"> {
let summary =
"A module-level attribute that holds the optimization information";
let description = [{
The `#cir.opt_info` attribute holds optimization related information. For
now this attribute is a module-level attribute that gets attached to the
module operation during CIRGen.

The `level` parameter gives the optimization level. It must be an integer
between 0 and 3, inclusive. It corresponds to the `OptimizationLevel` field
within the `clang::CodeGenOptions` structure.

The `size` parameter gives the code size optimization level. It must be an
integer between 0 and 2, inclusive. It corresponds to the `OptimizeSize`
field within the `clang::CodeGenOptions` structure.

The `level` and `size` parameters correspond to the optimization level
command line options passed to clang driver. The table below lists the
current correspondance relationship:

| Flag | `level` | `size` |
|------------------|---------|--------|
| `-O0` or nothing | 0 | 0 |
| `-O1` | 1 | 0 |
| `-O2` | 2 | 0 |
| `-O3` | 3 | 0 |
| `-Os` | 2 | 1 |
| `-Oz` | 2 | 2 |

Examples:

```mlir
#cir.opt_info<level = 2, size = 0> // -O2
```
}];

let parameters = (ins "unsigned":$level, "unsigned":$size);

let assemblyFormat = [{
`<` struct(params) `>`
}];
let genVerifyDecl = 1;
}

//===----------------------------------------------------------------------===//
// BoolAttr
//===----------------------------------------------------------------------===//
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRDialect.td
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def CIR_Dialect : Dialect {

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

void registerAttributes();
void registerTypes();
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,

theModule->setAttr(cir::CIRDialect::getTripleAttrName(),
builder.getStringAttr(getTriple().str()));

if (cgo.OptimizationLevel > 0 || cgo.OptimizeSize > 0)
theModule->setAttr(cir::CIRDialect::getOptInfoAttrName(),
cir::OptInfoAttr::get(&mlirContext,
cgo.OptimizationLevel,
cgo.OptimizeSize));
}

CIRGenModule::~CIRGenModule() = default;
Expand Down
15 changes: 15 additions & 0 deletions clang/lib/CIR/Dialect/IR/CIRAttrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ void CIRDialect::printAttribute(Attribute attr, DialectAsmPrinter &os) const {
llvm_unreachable("unexpected CIR type kind");
}

//===----------------------------------------------------------------------===//
// OptInfoAttr definitions
//===----------------------------------------------------------------------===//

LogicalResult OptInfoAttr::verify(function_ref<InFlightDiagnostic()> emitError,
unsigned level, unsigned size) {
if (level > 3)
return emitError()
<< "optimization level must be between 0 and 3 inclusive";
if (size > 2)
return emitError()
<< "size optimization level must be between 0 and 2 inclusive";
return success();
}

//===----------------------------------------------------------------------===//
// ConstPtrAttr definitions
//===----------------------------------------------------------------------===//
Expand Down
22 changes: 22 additions & 0 deletions clang/test/CIR/CodeGen/opt-info-attr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CHECK-O0
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -O1 -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CHECK-O1
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -O2 -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CHECK-O2
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -O3 -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CHECK-O3
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Os -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CHECK-Os
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Oz -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CHECK-Oz

void f() {}

// CHECK-O0: module attributes
// CHECK-O0-NOT: cir.opt_info
// CHECK-O1: module attributes {{.+}}cir.opt_info = #cir.opt_info<level = 1, size = 0>{{.+}}
// CHECK-O2: module attributes {{.+}}cir.opt_info = #cir.opt_info<level = 2, size = 0>{{.+}}
// CHECK-O3: module attributes {{.+}}cir.opt_info = #cir.opt_info<level = 3, size = 0>{{.+}}
// CHECK-Os: module attributes {{.+}}cir.opt_info = #cir.opt_info<level = 2, size = 1>{{.+}}
// CHECK-Oz: module attributes {{.+}}cir.opt_info = #cir.opt_info<level = 2, size = 2>{{.+}}
Loading