-
Notifications
You must be signed in to change notification settings - Fork 14.6k
[MLIR][LLVM] Import dereferenceable metadata from LLVM IR #130974
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -330,6 +330,43 @@ def AliasAnalysisOpInterface : OpInterface<"AliasAnalysisOpInterface"> { | |
]; | ||
} | ||
|
||
def DereferenceableOpInterface : OpInterface<"DereferenceableOpInterface"> { | ||
let description = [{ | ||
An interface for memory operations that can carry dereferenceable metadata. | ||
It provides setters and getters for the operation's dereferenceable | ||
attributes. The default implementations of the interface methods expect | ||
the operation to have an attribute of type DereferenceableAttr. | ||
}]; | ||
|
||
let cppNamespace = "::mlir::LLVM"; | ||
let verify = [{ return detail::verifyDereferenceableOpInterface($_op); }]; | ||
|
||
let methods = [ | ||
InterfaceMethod< | ||
/*desc=*/ "Returns the dereferenceable attribute or nullptr", | ||
/*returnType=*/ "::mlir::LLVM::DereferenceableAttr", | ||
/*methodName=*/ "getDereferenceableOrNull", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm curious - why is this called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dereferenceable attribute is optional on both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I vaguely remember this naming convention was chosen to avoid a name clash with a tablegen generated function. I could be wrong though. |
||
/*args=*/ (ins), | ||
/*methodBody=*/ [{}], | ||
/*defaultImpl=*/ [{ | ||
auto op = cast<ConcreteOp>(this->getOperation()); | ||
return op.getDereferenceableAttr(); | ||
}] | ||
>, | ||
InterfaceMethod< | ||
/*desc=*/ "Sets the dereferenceable attribute", | ||
/*returnType=*/ "void", | ||
/*methodName=*/ "setDereferenceable", | ||
/*args=*/ (ins "::mlir::LLVM::DereferenceableAttr":$attr), | ||
/*methodBody=*/ [{}], | ||
/*defaultImpl=*/ [{ | ||
auto op = cast<ConcreteOp>(this->getOperation()); | ||
op.setDereferenceableAttr(attr); | ||
}] | ||
> | ||
]; | ||
} | ||
|
||
def FPExceptionBehaviorOpInterface : OpInterface<"FPExceptionBehaviorOpInterface"> { | ||
let description = [{ | ||
An interface for operations receiving an exception behavior attribute | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// RUN: mlir-opt --allow-unregistered-dialect -split-input-file -verify-diagnostics %s | ||
|
||
llvm.func @deref(%arg0: !llvm.ptr) { | ||
// expected-error @below {{op expected op to return a single LLVM pointer type}} | ||
%0 = llvm.load %arg0 dereferenceable<bytes = 8> {alignment = 8 : i64} : !llvm.ptr -> i64 | ||
llvm.return | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
; RUN: mlir-translate -import-llvm -split-input-file %s | FileCheck %s | ||
|
||
define void @deref(i64 %0, ptr %1) { | ||
; CHECK: llvm.inttoptr | ||
; CHECK-SAME: dereferenceable<bytes = 4> | ||
%3 = inttoptr i64 %0 to ptr, !dereferenceable !0 | ||
; CHECK: llvm.load | ||
; CHECK-SAME: dereferenceable<bytes = 8> | ||
%4 = load ptr, ptr %1, align 8, !dereferenceable !1 | ||
ret void | ||
} | ||
|
||
define void @deref_or_null(i64 %0, ptr %1) { | ||
; CHECK: llvm.inttoptr | ||
; CHECK-SAME: dereferenceable<bytes = 4, mayBeNull = true> | ||
%3 = inttoptr i64 %0 to ptr, !dereferenceable_or_null !0 | ||
; CHECK: llvm.load | ||
; CHECK-SAME: dereferenceable<bytes = 8, mayBeNull = true> | ||
%4 = load ptr, ptr %1, align 8, !dereferenceable_or_null !1 | ||
ret void | ||
} | ||
|
||
!0 = !{i64 4} | ||
!1 = !{i64 8} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// RUN: mlir-translate -mlir-to-llvmir -split-input-file %s | FileCheck %s | ||
|
||
llvm.func @deref(%arg0: i64, %arg1: !llvm.ptr) { | ||
// CHECK: inttoptr {{.*}} !dereferenceable [[D0:![0-9]+]] | ||
%0 = llvm.inttoptr %arg0 dereferenceable<bytes = 4> : i64 to !llvm.ptr | ||
%1 = llvm.load %0 {alignment = 4 : i64} : !llvm.ptr -> i32 | ||
// CHECK: load {{.*}} !dereferenceable [[D1:![0-9]+]] | ||
%2 = llvm.load %arg1 dereferenceable<bytes = 8> {alignment = 8 : i64} : !llvm.ptr -> !llvm.ptr | ||
llvm.store %1, %2 {alignment = 4 : i64} : i32, !llvm.ptr | ||
llvm.return | ||
} | ||
|
||
llvm.func @deref_or_null(%arg0: i64, %arg1: !llvm.ptr) { | ||
// CHECK: inttoptr {{.*}} !dereferenceable_or_null [[D0]] | ||
%0 = llvm.inttoptr %arg0 dereferenceable<bytes = 4, mayBeNull = true> : i64 to !llvm.ptr | ||
%1 = llvm.load %0 {alignment = 4 : i64} : !llvm.ptr -> i32 | ||
// CHECK: load {{.*}} !dereferenceable_or_null [[D1]] | ||
%2 = llvm.load %arg1 dereferenceable<bytes = 8, mayBeNull = true> {alignment = 8 : i64} : !llvm.ptr -> !llvm.ptr | ||
llvm.store %1, %2 {alignment = 4 : i64} : i32, !llvm.ptr | ||
llvm.return | ||
} | ||
|
||
// CHECK: [[D0]] = !{i64 4} | ||
// CHECK: [[D1]] = !{i64 8} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally we would add a check to the LoadOp verifier that checks the load returns a pointer if the attribute is set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I've added an interface verifier which performs the check.