[mlir] Add support for alignment annotations to the LLVM dialect to LLVM translation.

Summary:
With this change, a function argument attribute of the form
"llvm.align" = <int> will be translated to the corresponding align
attribute in LLVM by the ModuleConversion.

Differential Revision: https://reviews.llvm.org/D82161
This commit is contained in:
Stephan Herhut 2020-06-19 10:59:07 +02:00
parent 1e8e1ec00c
commit 2416e28c25
5 changed files with 42 additions and 0 deletions

View File

@ -1719,6 +1719,10 @@ LogicalResult LLVMDialect::verifyRegionArgAttribute(Operation *op,
if (argAttr.first == "llvm.noalias" && !argAttr.second.isa<BoolAttr>())
return op->emitError()
<< "llvm.noalias argument attribute of non boolean type";
// Check that llvm.align is an integer attribute.
if (argAttr.first == "llvm.align" && !argAttr.second.isa<IntegerAttr>())
return op->emitError()
<< "llvm.align argument attribute of non integer type";
return success();
}

View File

@ -724,6 +724,18 @@ LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) {
if (attr.getValue())
llvmArg.addAttr(llvm::Attribute::AttrKind::NoAlias);
}
if (auto attr = func.getArgAttrOfType<IntegerAttr>(argIdx, "llvm.align")) {
// NB: Attribute already verified to be int, so check if we can indeed
// attach the attribute to this argument, based on its type.
auto argTy = mlirArg.getType().dyn_cast<LLVM::LLVMType>();
if (!argTy.getUnderlyingType()->isPointerTy())
return func.emitError(
"llvm.align attribute attached to LLVM non-pointer argument");
llvmArg.addAttrs(
llvm::AttrBuilder().addAlignmentAttr(llvm::Align(attr.getInt())));
}
valueMapping[mlirArg] = &llvmArg;
argIdx++;
}

View File

@ -5,6 +5,13 @@ func @invalid_noalias(%arg0: !llvm.i32 {llvm.noalias = 3}) {
"llvm.return"() : () -> ()
}
// -----
// expected-error@+1{{llvm.align argument attribute of non integer type}}
func @invalid_align(%arg0: !llvm.i32 {llvm.align = "foo"}) {
"llvm.return"() : () -> ()
}
////////////////////////////////////////////////////////////////////////////////
// Check that parser errors are properly produced and do not crash the compiler.

View File

@ -7,6 +7,20 @@ func @foo() {
// -----
// expected-error @+1 {{llvm.noalias attribute attached to LLVM non-pointer argument}}
llvm.func @invalid_noalias(%arg0 : !llvm.float {llvm.noalias = true}) -> !llvm.float {
llvm.return %arg0 : !llvm.float
}
// -----
// expected-error @+1 {{llvm.align attribute attached to LLVM non-pointer argument}}
llvm.func @invalid_align(%arg0 : !llvm.float {llvm.align = 4}) -> !llvm.float {
llvm.return %arg0 : !llvm.float
}
// -----
llvm.func @no_nested_struct() -> !llvm<"[2 x [2 x [2 x {i32}]]]"> {
// expected-error @+1 {{struct types are not supported in constants}}
%0 = llvm.mlir.constant(dense<[[[1, 2], [3, 4]], [[42, 43], [44, 45]]]> : tensor<2x2x2xi32>) : !llvm<"[2 x [2 x [2 x {i32}]]]">

View File

@ -927,6 +927,11 @@ llvm.func @llvm_noalias(%arg0: !llvm<"float*"> {llvm.noalias = true}) {
llvm.return
}
// CHECK-LABEL: define void @llvm_align(float* align 4 {{%*.}})
llvm.func @llvm_align(%arg0: !llvm<"float*"> {llvm.align = 4}) {
llvm.return
}
// CHECK-LABEL: @llvm_varargs(...)
llvm.func @llvm_varargs(...)