Skip to content

Commit bba1ab7

Browse files
committed
Refactor intrinsic handling: rename namespace to CIRIntrinsic and introduce IntrinsicDescriptor struct for better intrinsic management
1 parent c7a074b commit bba1ab7

File tree

7 files changed

+858
-414
lines changed

7 files changed

+858
-414
lines changed

clang/include/clang/CIR/Dialect/IR/CIRIntrinsics.h

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class LLVMIntrinsicCallOp;
5050
class FuncOp;
5151
// FIXME: Unsure if we need a proper function type
5252

53-
namespace Intrinsic {
53+
namespace CIRIntrinsic {
5454

5555
// Abstraction for the arguments of the noalias intrinsics
5656
static const int NoAliasScopeDeclScopeArg = 0;
@@ -68,6 +68,26 @@ enum IndependentIntrinsics : unsigned {
6868
#undef GET_INTRINSIC_ENUM_VALUES
6969
};
7070

71+
// Simple descriptor struct that holds essential intrinsic information
72+
// In order to build CIRIntrinsicCallOp
73+
struct IntrinsicDescriptor {
74+
mlir::StringAttr name; // Mangled name attribute
75+
mlir::Type resultType; // Return type for the intrinsic
76+
ID id; // Original intrinsic ID (optional)
77+
78+
// Basic constructor
79+
IntrinsicDescriptor(mlir::StringAttr name, mlir::Type resultType,
80+
ID id = not_intrinsic)
81+
: name(name), resultType(resultType), id(id) {}
82+
83+
// Default constructor for empty/invalid descriptors
84+
IntrinsicDescriptor()
85+
: name(nullptr), resultType(nullptr), id(not_intrinsic) {}
86+
87+
// Check if descriptor is valid
88+
bool isValid() const { return name && resultType; }
89+
};
90+
7191
/// Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
7292
/// Note, this version is for intrinsics with no overloads. Use the other
7393
/// version of getName if overloads are required.
@@ -92,11 +112,15 @@ std::string getName(ID Id, llvm::ArrayRef<mlir::Type> Tys, mlir::ModuleOp M,
92112
std::string getNameNoUnnamedTypes(ID Id, llvm::ArrayRef<mlir::Type> Tys);
93113

94114
/// Return the function type for an intrinsic.
95-
mlir::Type *getType(mlir::MLIRContext& Context, ID id,
96-
llvm::ArrayRef<mlir::Type> Tys = {});
115+
// mlir::Type getType(mlir::MLIRContext &Context, ID id,
116+
// llvm::ArrayRef<mlir::Type> Tys = {});
117+
118+
// Get both return type and parameter types in one call
119+
mlir::Type getType(mlir::MLIRContext &Context, ID id,
120+
llvm::ArrayRef<mlir::Type> Tys);
97121

98122
/// Returns true if the intrinsic can be overloaded.
99-
bool isOverloaded(ID id);
123+
bool isOverloaded(ID id); // NYI
100124

101125
ID lookupIntrinsicID(llvm::StringRef Name);
102126

@@ -117,18 +141,18 @@ ID lookupIntrinsicID(llvm::StringRef Name);
117141
/// using iAny, fAny, vAny, or pAny). For a declaration of an overloaded
118142
/// intrinsic, Tys must provide exactly one type for each overloaded type in
119143
/// the intrinsic.
120-
LLVMIntrinsicCallOp getOrInsertDeclaration(mlir::ModuleOp M, ID id,
121-
llvm::ArrayRef<mlir::Type> Tys = {});
144+
IntrinsicDescriptor getOrInsertDeclaration(mlir::ModuleOp M, ID id,
145+
llvm::ArrayRef<mlir::Type> Tys = {});
122146

123147
/// Look up the Function declaration of the intrinsic \p id in the Module
124148
/// \p M and return it if it exists. Otherwise, return nullptr. This version
125149
/// supports non-overloaded intrinsics.
126-
LLVMIntrinsicCallOp getDeclarationIfExists(const mlir::ModuleOp *M, ID id);
150+
IntrinsicDescriptor getDeclarationIfExists(const mlir::ModuleOp *M, ID id);
127151

128152
/// This version supports overloaded intrinsics.
129-
LLVMIntrinsicCallOp getDeclarationIfExists(mlir::ModuleOp M, ID id,
130-
llvm::ArrayRef<mlir::Type> Tys,
131-
mlir::Type FT = nullptr);
153+
IntrinsicDescriptor getDeclarationIfExists(mlir::ModuleOp M, ID id,
154+
llvm::ArrayRef<mlir::Type> Tys,
155+
mlir::Type FT = nullptr);
132156

133157
/// Map a Clang builtin name to an intrinsic ID.
134158
ID getIntrinsicForClangBuiltin(llvm::StringRef TargetPrefix,
@@ -182,7 +206,7 @@ bool matchIntrinsicVarArg(
182206
///
183207
/// Returns false if the given ID and function type combination is not a
184208
/// valid intrinsic call.
185-
bool getIntrinsicSignature(Intrinsic::ID, mlir::Type FT,
209+
bool getIntrinsicSignature(ID, mlir::Type FT,
186210
llvm::SmallVectorImpl<mlir::Type> &ArgTys);
187211

188212
/// Same as previous, but accepts a Function instead of ID and FunctionType.
@@ -194,7 +218,7 @@ bool getIntrinsicSignature(FuncOp F, llvm::SmallVectorImpl<mlir::Type> &ArgTys);
194218
// or of the wrong kind will be renamed by adding ".renamed" to the name.
195219
std::optional<LLVMIntrinsicCallOp> remangleIntrinsicFunction(FuncOp F);
196220

197-
} // namespace Intrinsic
221+
} // namespace CIRIntrinsic
198222
} // namespace cir
199223

200224
#endif // LLVM_CLANG_CIR_DIALECT_INTRINSICS_H

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "Address.h"
1313
#include "CIRGenRecordLayout.h"
1414
#include "CIRGenTypeCache.h"
15+
#include "clang/CIR/Dialect/IR/CIRIntrinsics.h"
1516
#include "clang/CIR/MissingFeatures.h"
1617

1718
#include "clang/AST/Decl.h"
@@ -749,6 +750,17 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
749750
return create<cir::VTTAddrPointOp>(loc, retTy, sym, mlir::Value{}, offset);
750751
}
751752

753+
mlir::Value
754+
createIntrinsicCall(mlir::Location loc,
755+
cir::CIRIntrinsic::IntrinsicDescriptor descriptor,
756+
llvm::ArrayRef<mlir::Value> args = {}) {
757+
assert(descriptor.isValid() && "invalid intrinsic descriptor");
758+
759+
return create<cir::LLVMIntrinsicCallOp>(loc, descriptor.name,
760+
descriptor.resultType, args)
761+
.getResult();
762+
}
763+
752764
// FIXME(cir): CIRGenBuilder class should have an attribute with a reference
753765
// to the module so that we don't have search for it or pass it around.
754766
// FIXME(cir): Track a list of globals, or at least the last one inserted, so

clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "clang/Basic/TargetBuiltins.h"
2626
#include "clang/CIR/Dialect/IR/CIRDialect.h"
2727
#include "clang/CIR/Dialect/IR/CIRTypes.h"
28+
#include "llvm/IR/Intrinsics.h"
29+
#include "llvm/IR/IntrinsicsX86.h"
2830
#include "llvm/Support/ErrorHandling.h"
2931

3032
using namespace clang;
@@ -98,51 +100,33 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned BuiltinID,
98100
llvm_unreachable("_mm_prefetch NYI");
99101
}
100102
case X86::BI_mm_clflush: {
101-
mlir::Type voidTy = cir::VoidType::get(&getMLIRContext());
102-
return builder
103-
.create<cir::LLVMIntrinsicCallOp>(
104-
getLoc(E->getExprLoc()), builder.getStringAttr("x86.sse2.clflush"),
105-
voidTy, Ops[0])
106-
.getResult();
103+
return builder.createIntrinsicCall(
104+
getLoc(E->getExprLoc()),
105+
CGM.getIntrinsic(llvm::Intrinsic::x86_sse2_clflush), Ops[0]);
107106
}
108107
case X86::BI_mm_lfence: {
109-
mlir::Type voidTy = cir::VoidType::get(&getMLIRContext());
110-
return builder
111-
.create<cir::LLVMIntrinsicCallOp>(
112-
getLoc(E->getExprLoc()), builder.getStringAttr("x86.sse2.lfence"),
113-
voidTy)
114-
.getResult();
108+
return builder.createIntrinsicCall(
109+
getLoc(E->getExprLoc()),
110+
CGM.getIntrinsic(llvm::Intrinsic::x86_sse2_lfence));
115111
}
116112
case X86::BI_mm_pause: {
117-
mlir::Type voidTy = cir::VoidType::get(&getMLIRContext());
118-
return builder
119-
.create<cir::LLVMIntrinsicCallOp>(
120-
getLoc(E->getExprLoc()), builder.getStringAttr("x86.sse2.pause"),
121-
voidTy)
122-
.getResult();
113+
return builder.createIntrinsicCall(
114+
getLoc(E->getExprLoc()),
115+
CGM.getIntrinsic(llvm::Intrinsic::x86_sse2_pause));
123116
}
124117
case X86::BI_mm_mfence: {
125-
mlir::Type voidTy = cir::VoidType::get(&getMLIRContext());
126-
return builder
127-
.create<cir::LLVMIntrinsicCallOp>(
128-
getLoc(E->getExprLoc()), builder.getStringAttr("x86.sse2.mfence"),
129-
voidTy)
130-
.getResult();
118+
return builder.createIntrinsicCall(
119+
getLoc(E->getExprLoc()),
120+
CGM.getIntrinsic(llvm::Intrinsic::x86_sse2_mfence));
131121
}
132122
case X86::BI_mm_sfence: {
133-
mlir::Type voidTy = cir::VoidType::get(&getMLIRContext());
134-
return builder
135-
.create<cir::LLVMIntrinsicCallOp>(
136-
getLoc(E->getExprLoc()), builder.getStringAttr("x86.sse.sfence"),
137-
voidTy)
138-
.getResult();
123+
return builder.createIntrinsicCall(
124+
getLoc(E->getExprLoc()),
125+
CGM.getIntrinsic(llvm::Intrinsic::x86_sse_sfence));
139126
}
140127
case X86::BI__rdtsc: {
141-
mlir::Type intTy = cir::IntType::get(&getMLIRContext(), 64, false);
142-
return builder
143-
.create<cir::LLVMIntrinsicCallOp>(
144-
getLoc(E->getExprLoc()), builder.getStringAttr("x86.rdtsc"), intTy)
145-
.getResult();
128+
return builder.createIntrinsicCall(
129+
getLoc(E->getExprLoc()), CGM.getIntrinsic(llvm::Intrinsic::x86_rdtsc));
146130
}
147131
case X86::BI__builtin_ia32_rdtscp: {
148132
llvm_unreachable("__rdtscp NYI");

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,9 +2478,9 @@ void CIRGenModule::emitAliasForGlobal(StringRef mangledName,
24782478
setCommonAttributes(aliasGD, alias);
24792479
}
24802480

2481-
cir::LLVMIntrinsicCallOp
2481+
cir::CIRIntrinsic::IntrinsicDescriptor
24822482
CIRGenModule::getIntrinsic(unsigned IID, ArrayRef<mlir::Type> Tys) {
2483-
return cir::Intrinsic::getOrInsertDeclaration(getModule(), (cir::Intrinsic::ID)IID, Tys);
2483+
return cir::CIRIntrinsic::getOrInsertDeclaration(getModule(), (cir::CIRIntrinsic::ID)IID, Tys);
24842484
}
24852485

24862486
mlir::Type CIRGenModule::convertType(QualType type) {

clang/lib/CIR/CodeGen/CIRGenModule.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
3333
#include "clang/CIR/Dialect/IR/CIRDataLayout.h"
3434
#include "clang/CIR/Dialect/IR/CIRDialect.h"
35+
#include "clang/CIR/Dialect/IR/CIRIntrinsics.h"
3536
#include "clang/CIR/Dialect/IR/CIROpsEnums.h"
3637
#include "clang/CIR/Dialect/IR/CIRTypes.h"
3738
#include "clang/CIR/Interfaces/CIROpInterfaces.h"
@@ -655,8 +656,8 @@ class CIRGenModule : public CIRGenTypeCache {
655656
void emitAliasForGlobal(llvm::StringRef mangledName, mlir::Operation *op,
656657
GlobalDecl aliasGD, cir::FuncOp aliasee,
657658
cir::GlobalLinkageKind linkage);
658-
cir::LLVMIntrinsicCallOp getIntrinsic(unsigned IID,
659-
ArrayRef<mlir::Type> Tys);
659+
cir::CIRIntrinsic::IntrinsicDescriptor
660+
getIntrinsic(unsigned IID, ArrayRef<mlir::Type> Tys = {});
660661
mlir::Type convertType(clang::QualType type);
661662

662663
/// Set the visibility for the given global.

0 commit comments

Comments
 (0)