Skip to content

Commit ae33bcc

Browse files
committed
[MLIR][LLVM] Add explicit target_cpu attribute to llvm.func
This patch adds the target_cpu attribute to llvm.func MLIR operations and updates the translation to/from LLVM IR to match "target-cpu" function attributes.
1 parent a9bfad2 commit ae33bcc

File tree

7 files changed

+36
-1
lines changed

7 files changed

+36
-1
lines changed

mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,7 @@ def LLVM_LLVMFuncOp : LLVM_Op<"func", [
14271427
OptionalAttr<I64Attr>:$alignment,
14281428
OptionalAttr<LLVM_VScaleRangeAttr>:$vscale_range,
14291429
OptionalAttr<FramePointerKindAttr>:$frame_pointer,
1430+
OptionalAttr<StrAttr>:$target_cpu,
14301431
OptionalAttr<LLVM_TargetFeaturesAttr>:$target_features
14311432
);
14321433

mlir/lib/Target/LLVMIR/ModuleImport.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,11 @@ void ModuleImport::processFunctionAttributes(llvm::Function *func,
17441744
.value()));
17451745
}
17461746

1747+
if (llvm::Attribute attr = func->getFnAttribute("target-cpu");
1748+
attr.isStringAttribute()) {
1749+
funcOp.setTargetCpuAttr(StringAttr::get(context, attr.getValueAsString()));
1750+
}
1751+
17471752
if (llvm::Attribute attr = func->getFnAttribute("target-features");
17481753
attr.isStringAttribute()) {
17491754
funcOp.setTargetFeaturesAttr(

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,9 @@ LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) {
11041104
if (func.getArmPreservesZa())
11051105
llvmFunc->addFnAttr("aarch64_pstate_za_preserved");
11061106

1107+
if (auto targetCpu = func.getTargetCpu())
1108+
llvmFunc->addFnAttr("target-cpu", *targetCpu);
1109+
11071110
if (auto targetFeatures = func.getTargetFeatures())
11081111
llvmFunc->addFnAttr("target-features", targetFeatures->getFeaturesString());
11091112

mlir/test/Conversion/FuncToLLVM/convert-funcs.mlir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ func.func @variadic_func(%arg0: i32) attributes { "func.varargs" = true } {
6161
return
6262
}
6363

64+
// CHECK-LABEL: llvm.func @target_cpu()
65+
// CHECK-SAME: target_cpu = "gfx90a"
66+
func.func private @target_cpu() attributes { "target_cpu" = "gfx90a" }
67+
68+
// CHECK-LABEL: llvm.func @target_features()
69+
// CHECK-SAME: target_features = #llvm.target_features<["+sme", "+sve"]>
70+
func.func private @target_features() attributes {
71+
"target_features" = #llvm.target_features<["+sme", "+sve"]>
72+
}
73+
6474
// -----
6575

6676
// CHECK-LABEL: llvm.func @private_callee

mlir/test/Dialect/Bufferization/Transforms/one-shot-module-bufferize-analysis.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ func.func @insert_slice_chain(
772772
// CHECK-SAME: bufferization.access = "none"
773773
%arg2: tensor<62x90xf32> {bufferization.buffer_layout = affine_map<(d0, d1) -> (d0, d1)>, bufferization.writable = true})
774774
// CHECK-SAME: bufferization.access = "write"
775-
-> tensor<62x90xf32> attributes {passthrough = [["target-cpu", "skylake-avx512"], ["prefer-vector-width", "512"]]}
775+
-> tensor<62x90xf32> attributes {passthrough = [["prefer-vector-width", "512"]], target_cpu = "skylake-avx512"}
776776
{
777777
%c0 = arith.constant 0 : index
778778
%cst = arith.constant 0.000000e+00 : f32
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
; RUN: mlir-translate -import-llvm -split-input-file %s | FileCheck %s
2+
3+
; CHECK-LABEL: llvm.func @target_cpu()
4+
; CHECK-SAME: target_cpu = "gfx90a"
5+
define void @target_cpu() #0 {
6+
ret void
7+
}
8+
9+
attributes #0 = { "target-cpu"="gfx90a" }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
2+
3+
// CHECK: define void @target_cpu() #[[ATTRS:.*]] {
4+
// CHECK: attributes #[[ATTRS]] = { "target-cpu"="gfx90a" }
5+
llvm.func @target_cpu() attributes {target_cpu = "gfx90a"} {
6+
llvm.return
7+
}

0 commit comments

Comments
 (0)