Skip to content
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

[mlir][llvmir] Added extra builders for CallInstrinsicOp #111664

Merged
merged 5 commits into from
Oct 10, 2024

Conversation

FMarno
Copy link
Contributor

@FMarno FMarno commented Oct 9, 2024

I've added the extra builders I would like to see for CallIntrinsicOp.
This is inspired by the comment from @antiagainst from here. I am also building Triton.

@llvmbot
Copy link
Member

llvmbot commented Oct 9, 2024

@llvm/pr-subscribers-mlir

Author: Finlay (FMarno)

Changes

I've added the extra builders I would like to see for CallIntrinsicOp.
This is inspired by the comment from @antiagainst from here. I am also building Triton.


Full diff: https://github.com/llvm/llvm-project/pull/111664.diff

2 Files Affected:

  • (modified) mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td (+5)
  • (modified) mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp (+19)
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
index 88e82ce48959b0..ff5199fe40d124 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
@@ -1943,6 +1943,11 @@ def LLVM_CallIntrinsicOp
     attr-dict
   }];
 
+  let builders = [
+    OpBuilder<(ins "Type": $results, "StringAttr":$intrin, "ValueRange":$args)>,
+    OpBuilder<(ins "TypeRange": $results, "StringAttr":$intrin, "ValueRange":$args, "FastmathFlagsAttr":$fastMathFlags)>
+    ];
+
   let hasVerifier = 1;
 }
 
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
index fb7024a14f8d4e..27cbd77f110326 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -3353,6 +3353,25 @@ struct LLVMOpAsmDialectInterface : public OpAsmDialectInterface {
 };
 } // namespace
 
+//===----------------------------------------------------------------------===//
+// CallIntrinsicOp
+//===----------------------------------------------------------------------===//
+
+void CallIntrinsicOp::build(OpBuilder &builder, OperationState &state,
+                            mlir::TypeRange results, mlir::StringAttr intrin,
+                            mlir::ValueRange args,
+                            mlir::LLVM::FastmathFlagsAttr fastMathFlags) {
+
+  build(builder, state, results, intrin, args, fastMathFlags,
+        llvm::ArrayRef<mlir::ValueRange>{});
+}
+void CallIntrinsicOp::build(OpBuilder &builder, OperationState &state,
+                            mlir::Type results, mlir::StringAttr intrin,
+                            mlir::ValueRange args) {
+  build(builder, state, {results}, intrin, args, FastmathFlagsAttr{},
+        llvm::ArrayRef<mlir::ValueRange>{});
+}
+
 //===----------------------------------------------------------------------===//
 // LinkerOptionsOp
 //===----------------------------------------------------------------------===//

@llvmbot
Copy link
Member

llvmbot commented Oct 9, 2024

@llvm/pr-subscribers-mlir-llvm

Author: Finlay (FMarno)

Changes

I've added the extra builders I would like to see for CallIntrinsicOp.
This is inspired by the comment from @antiagainst from here. I am also building Triton.


Full diff: https://github.com/llvm/llvm-project/pull/111664.diff

2 Files Affected:

  • (modified) mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td (+5)
  • (modified) mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp (+19)
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
index 88e82ce48959b0..ff5199fe40d124 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
@@ -1943,6 +1943,11 @@ def LLVM_CallIntrinsicOp
     attr-dict
   }];
 
+  let builders = [
+    OpBuilder<(ins "Type": $results, "StringAttr":$intrin, "ValueRange":$args)>,
+    OpBuilder<(ins "TypeRange": $results, "StringAttr":$intrin, "ValueRange":$args, "FastmathFlagsAttr":$fastMathFlags)>
+    ];
+
   let hasVerifier = 1;
 }
 
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
index fb7024a14f8d4e..27cbd77f110326 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -3353,6 +3353,25 @@ struct LLVMOpAsmDialectInterface : public OpAsmDialectInterface {
 };
 } // namespace
 
+//===----------------------------------------------------------------------===//
+// CallIntrinsicOp
+//===----------------------------------------------------------------------===//
+
+void CallIntrinsicOp::build(OpBuilder &builder, OperationState &state,
+                            mlir::TypeRange results, mlir::StringAttr intrin,
+                            mlir::ValueRange args,
+                            mlir::LLVM::FastmathFlagsAttr fastMathFlags) {
+
+  build(builder, state, results, intrin, args, fastMathFlags,
+        llvm::ArrayRef<mlir::ValueRange>{});
+}
+void CallIntrinsicOp::build(OpBuilder &builder, OperationState &state,
+                            mlir::Type results, mlir::StringAttr intrin,
+                            mlir::ValueRange args) {
+  build(builder, state, {results}, intrin, args, FastmathFlagsAttr{},
+        llvm::ArrayRef<mlir::ValueRange>{});
+}
+
 //===----------------------------------------------------------------------===//
 // LinkerOptionsOp
 //===----------------------------------------------------------------------===//

Copy link
Contributor

@gysit gysit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks it makes sense adding such builders!

Did you test it downstream? It is a bit hard to see from the code if all the necessary fields of the default constructor are set.

mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp Outdated Show resolved Hide resolved
mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td Outdated Show resolved Hide resolved
mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td Outdated Show resolved Hide resolved
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp Outdated Show resolved Hide resolved
Copy link
Member

@Lancern Lancern left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for improving this!

Comment on lines 1946 to 1949
let builders = [
OpBuilder<(ins "Type": $results, "StringAttr":$intrin, "ValueRange":$args)>,
OpBuilder<(ins "TypeRange": $results, "StringAttr":$intrin, "ValueRange":$args, "FastmathFlagsAttr":$fastMathFlags)>
];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could also include builders for intrinsics that do not have a result type?

OpBuilder<(ins "StringAttr":$intrin, "ValueRange":$args)>
OpBuilder<(ins "StringAttr":$intrin, "ValueRange":$args, "FastmathFlagsAttr":$fastMathFlags)>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review.
I've added those in. Do you know how I could confirm they work? I don't really have a use cause so I was just guessing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another approach might be adding a unit test under mlir/unittests/Dialect/LLVMIR. But it seems like few people ever do this ... Anyway this builder is fairly simple and I believe it should be OK if no reviewers have problems about it.

@FMarno
Copy link
Contributor Author

FMarno commented Oct 9, 2024

@gysit Thank you for the review, I've addressed all your comments.

Did you test it downstream? It is a bit hard to see from the code if all the necessary fields of the default constructor are set.

Yes, it works for code gen in Triton. If there is a better way to test builders then I'd love to know.

@FMarno FMarno requested review from gysit and Lancern October 9, 2024 16:24
Copy link
Contributor

@gysit gysit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo nits!

The only easy way to test builders is using them in a pass/canonicalization AFAIK. Since there are not many uses of this op upstream that does probably not work here.

@@ -3324,6 +3324,34 @@ LogicalResult CallIntrinsicOp::verify() {
return success();
}

void CallIntrinsicOp::build(OpBuilder &builder, OperationState &state,
mlir::StringAttr intrin, mlir::ValueRange args) {
build(builder, state, /*results=*/Type{}, intrin, args, FastmathFlagsAttr{},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
build(builder, state, /*results=*/Type{}, intrin, args, FastmathFlagsAttr{},
build(builder, state, /*results=*/TypeRange{}, intrin, args, FastmathFlagsAttr{},

I think this is a type range (probably Type converts to TypeRange)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I've addressed that.

void CallIntrinsicOp::build(OpBuilder &builder, OperationState &state,
mlir::StringAttr intrin, mlir::ValueRange args,
mlir::LLVM::FastmathFlagsAttr fastMathFlags) {
build(builder, state, /*results=*/Type{}, intrin, args, fastMathFlags,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
build(builder, state, /*results=*/Type{}, intrin, args, fastMathFlags,
build(builder, state, /*results=*/TypeRange{}, intrin, args, fastMathFlags,

@FMarno FMarno merged commit 741ad3a into llvm:main Oct 10, 2024
9 checks passed
DanielCChen pushed a commit to DanielCChen/llvm-project that referenced this pull request Oct 16, 2024
Extra builders for CallIntrinsicOp.
This is inspired by the comment from @antiagainst from
[here](llvm#108933 (comment)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants