Skip to content

Conversation

@banach-space
Copy link
Contributor

Extract emitIntrinsicCallOp, which was previously defined
independently in CIRGenBuiltinAArch64.cpp and CIRGenBuiltinX86.cpp, into
a shared header (CIRGenUtils.h).

Extract `emitIntrinsicCallOp`, which was previously defined
independently in CIRGenBuiltinAArch64.cpp and CIRGenBuiltinX86.cpp, into
a shared header (CIRGenUtils.h).
@llvmbot llvmbot added clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project labels Dec 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 17, 2025

@llvm/pr-subscribers-clang

Author: Andrzej Warzyński (banach-space)

Changes

Extract emitIntrinsicCallOp, which was previously defined
independently in CIRGenBuiltinAArch64.cpp and CIRGenBuiltinX86.cpp, into
a shared header (CIRGenUtils.h).


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

3 Files Affected:

  • (modified) clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp (+1-11)
  • (modified) clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp (+1-11)
  • (added) clang/lib/CIR/CodeGen/CIRGenUtils.h (+34)
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp
index e28b3c6cdc2ff..3e4b31ce3fbf2 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp
@@ -13,6 +13,7 @@
 
 #include "CIRGenBuilder.h"
 #include "CIRGenFunction.h"
+#include "CIRGenUtils.h"
 #include "clang/CIR/MissingFeatures.h"
 
 // TODO(cir): once all builtins are covered, decide whether we still
@@ -31,17 +32,6 @@ using namespace clang;
 using namespace clang::CIRGen;
 using namespace llvm;
 
-template <typename... Operands>
-static mlir::Value emitIntrinsicCallOp(CIRGenBuilderTy &builder,
-                                       mlir::Location loc, const StringRef str,
-                                       const mlir::Type &resTy,
-                                       Operands &&...op) {
-  return cir::LLVMIntrinsicCallOp::create(builder, loc,
-                                          builder.getStringAttr(str), resTy,
-                                          std::forward<Operands>(op)...)
-      .getResult();
-}
-
 // Generate vscale * scalingFactor
 static mlir::Value genVscaleTimesFactor(mlir::Location loc,
                                         CIRGenBuilderTy builder,
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp
index 72e6bea244802..3e13dea9b4811 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp
@@ -14,6 +14,7 @@
 #include "CIRGenBuilder.h"
 #include "CIRGenFunction.h"
 #include "CIRGenModule.h"
+#include "CIRGenUtils.h"
 #include "mlir/IR/Location.h"
 #include "mlir/IR/ValueRange.h"
 #include "clang/Basic/Builtins.h"
@@ -25,17 +26,6 @@
 using namespace clang;
 using namespace clang::CIRGen;
 
-template <typename... Operands>
-static mlir::Value emitIntrinsicCallOp(CIRGenBuilderTy &builder,
-                                       mlir::Location loc, const StringRef str,
-                                       const mlir::Type &resTy,
-                                       Operands &&...op) {
-  return cir::LLVMIntrinsicCallOp::create(builder, loc,
-                                          builder.getStringAttr(str), resTy,
-                                          std::forward<Operands>(op)...)
-      .getResult();
-}
-
 // OG has unordered comparison as a form of optimization in addition to
 // ordered comparison, while CIR doesn't.
 //
diff --git a/clang/lib/CIR/CodeGen/CIRGenUtils.h b/clang/lib/CIR/CodeGen/CIRGenUtils.h
new file mode 100644
index 0000000000000..054c5d6ade5e2
--- /dev/null
+++ b/clang/lib/CIR/CodeGen/CIRGenUtils.h
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Miscellaneous utility functions used by CIR code-gen.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENUTILS_H
+#define LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENUTILS_H
+
+#include "CIRGenBuilder.h"
+#include "mlir/IR/Value.h"
+#include "llvm/ADT/StringRef.h"
+
+namespace clang::CIRGen {
+
+template <typename... Operands>
+mlir::Value emitIntrinsicCallOp(CIRGenBuilderTy &builder, mlir::Location loc,
+                                const llvm::StringRef str,
+                                const mlir::Type &resTy, Operands &&...op) {
+  return cir::LLVMIntrinsicCallOp::create(builder, loc,
+                                          builder.getStringAttr(str), resTy,
+                                          std::forward<Operands>(op)...)
+      .getResult();
+}
+
+} // namespace clang::CIRGen
+
+#endif

@llvmbot
Copy link
Member

llvmbot commented Dec 17, 2025

@llvm/pr-subscribers-clangir

Author: Andrzej Warzyński (banach-space)

Changes

Extract emitIntrinsicCallOp, which was previously defined
independently in CIRGenBuiltinAArch64.cpp and CIRGenBuiltinX86.cpp, into
a shared header (CIRGenUtils.h).


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

3 Files Affected:

  • (modified) clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp (+1-11)
  • (modified) clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp (+1-11)
  • (added) clang/lib/CIR/CodeGen/CIRGenUtils.h (+34)
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp
index e28b3c6cdc2ff..3e4b31ce3fbf2 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp
@@ -13,6 +13,7 @@
 
 #include "CIRGenBuilder.h"
 #include "CIRGenFunction.h"
+#include "CIRGenUtils.h"
 #include "clang/CIR/MissingFeatures.h"
 
 // TODO(cir): once all builtins are covered, decide whether we still
@@ -31,17 +32,6 @@ using namespace clang;
 using namespace clang::CIRGen;
 using namespace llvm;
 
-template <typename... Operands>
-static mlir::Value emitIntrinsicCallOp(CIRGenBuilderTy &builder,
-                                       mlir::Location loc, const StringRef str,
-                                       const mlir::Type &resTy,
-                                       Operands &&...op) {
-  return cir::LLVMIntrinsicCallOp::create(builder, loc,
-                                          builder.getStringAttr(str), resTy,
-                                          std::forward<Operands>(op)...)
-      .getResult();
-}
-
 // Generate vscale * scalingFactor
 static mlir::Value genVscaleTimesFactor(mlir::Location loc,
                                         CIRGenBuilderTy builder,
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp
index 72e6bea244802..3e13dea9b4811 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp
@@ -14,6 +14,7 @@
 #include "CIRGenBuilder.h"
 #include "CIRGenFunction.h"
 #include "CIRGenModule.h"
+#include "CIRGenUtils.h"
 #include "mlir/IR/Location.h"
 #include "mlir/IR/ValueRange.h"
 #include "clang/Basic/Builtins.h"
@@ -25,17 +26,6 @@
 using namespace clang;
 using namespace clang::CIRGen;
 
-template <typename... Operands>
-static mlir::Value emitIntrinsicCallOp(CIRGenBuilderTy &builder,
-                                       mlir::Location loc, const StringRef str,
-                                       const mlir::Type &resTy,
-                                       Operands &&...op) {
-  return cir::LLVMIntrinsicCallOp::create(builder, loc,
-                                          builder.getStringAttr(str), resTy,
-                                          std::forward<Operands>(op)...)
-      .getResult();
-}
-
 // OG has unordered comparison as a form of optimization in addition to
 // ordered comparison, while CIR doesn't.
 //
diff --git a/clang/lib/CIR/CodeGen/CIRGenUtils.h b/clang/lib/CIR/CodeGen/CIRGenUtils.h
new file mode 100644
index 0000000000000..054c5d6ade5e2
--- /dev/null
+++ b/clang/lib/CIR/CodeGen/CIRGenUtils.h
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Miscellaneous utility functions used by CIR code-gen.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENUTILS_H
+#define LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENUTILS_H
+
+#include "CIRGenBuilder.h"
+#include "mlir/IR/Value.h"
+#include "llvm/ADT/StringRef.h"
+
+namespace clang::CIRGen {
+
+template <typename... Operands>
+mlir::Value emitIntrinsicCallOp(CIRGenBuilderTy &builder, mlir::Location loc,
+                                const llvm::StringRef str,
+                                const mlir::Type &resTy, Operands &&...op) {
+  return cir::LLVMIntrinsicCallOp::create(builder, loc,
+                                          builder.getStringAttr(str), resTy,
+                                          std::forward<Operands>(op)...)
+      .getResult();
+}
+
+} // namespace clang::CIRGen
+
+#endif

@banach-space banach-space force-pushed the andrzej/cir/add_utility branch from f0a31d9 to 14428f3 Compare December 17, 2025 21:05
Copy link
Contributor

@andykaylor andykaylor left a comment

Choose a reason for hiding this comment

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

This is basically what I had in mind. I was thinking of it as specific to the builtin handling, but I suppose there is no reason it needs to be.

This function in particular has been a bit slippery. I like it as a helper function. It feels like it could just be moved to CIRGenFunction, but if we did that we'd have to pass cgf around to a lot of other helper functions that otherwise only need the builder.

@HendrikHuebner You did a lot of the work to cleanup the builtin handling code, including introducing this function. What do you think about this?

@HendrikHuebner
Copy link
Contributor

This is basically what I had in mind. I was thinking of it as specific to the builtin handling, but I suppose there is no reason it needs to be.

This function in particular has been a bit slippery. I like it as a helper function. It feels like it could just be moved to CIRGenFunction, but if we did that we'd have to pass cgf around to a lot of other helper functions that otherwise only need the builder.

@HendrikHuebner You did a lot of the work to cleanup the builtin handling code, including introducing this function. What do you think about this?

Moving some of the helper functions to a different file sounds like a good idea. We could also consider making this particular function a member of CIRGenBuilderTy.

@bcardosolopes
Copy link
Member

I like this direction, but if we expect this to grow, it might be worth to have a CIRGenBuiltinTargetCommon.cpp for the implementations of such helpers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants