1616#include " clang/Basic/Builtins.h"
1717#include " clang/Basic/TargetBuiltins.h"
1818#include " clang/CIR/MissingFeatures.h"
19- #include " llvm/IR/IntrinsicsX86.h"
20-
21- #define UNIMPLEMENTED_BUILTIN () \
22- do { \
23- cgm.errorNYI (e->getSourceRange (), \
24- std::string (" unimplemented X86 builtin call: " ) + \
25- getContext ().BuiltinInfo .getName (builtinID)); \
26- return {}; \
27- } while (0 )
2819
2920using namespace clang ;
3021using namespace clang ::CIRGen;
3122
32- // / Get integer from a mlir::Value that is an int constant or a constant op.
33- static int64_t getIntValueFromConstOp (mlir::Value val) {
34- return val.getDefiningOp <cir::ConstantOp>().getIntValue ().getSExtValue ();
23+ template <typename ... Operands>
24+ static mlir::Value emitIntrinsicCallOp (CIRGenFunction &cgf, const CallExpr *e,
25+ const std::string &str,
26+ const mlir::Type &resTy,
27+ Operands &&...op) {
28+ CIRGenBuilderTy &builder = cgf.getBuilder ();
29+ mlir::Location location = cgf.getLoc (e->getExprLoc ());
30+ return cir::LLVMIntrinsicCallOp::create (builder, location,
31+ builder.getStringAttr (str), resTy,
32+ std::forward<Operands>(op)...)
33+ .getResult ();
34+ }
35+
36+ static mlir::Value emitPrefetch (CIRGenFunction &cgf, const CallExpr *e,
37+ mlir::Value &addr, int64_t hint) {
38+ CIRGenBuilderTy &builder = cgf.getBuilder ();
39+ mlir::Location location = cgf.getLoc (e->getExprLoc ());
40+ mlir::Type voidTy = builder.getVoidTy ();
41+ mlir::Value address = builder.createPtrBitcast (addr, voidTy);
42+ mlir::Value rw = builder.getSignedInt (location, (hint >> 2 ) & 0x1 , 32 );
43+ mlir::Value locality = builder.getSignedInt (location, hint & 0x3 , 32 );
44+ mlir::Value data = builder.getSignedInt (location, 1 , 32 );
45+
46+ return emitIntrinsicCallOp (cgf, e, " prefetch" , voidTy,
47+ mlir::ValueRange{address, rw, locality, data});
3548}
3649
3750mlir::Value CIRGenFunction::emitX86BuiltinExpr (unsigned builtinID,
@@ -56,76 +69,33 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID,
5669 // Find out if any arguments are required to be integer constant expressions.
5770 assert (!cir::MissingFeatures::handleBuiltinICEArguments ());
5871
59- llvm::SmallVector<mlir::Value, 4 > ops;
72+ llvm::SmallVector<mlir::Value> ops;
6073
6174 // Find out if any arguments are required to be integer constant expressions.
62- unsigned ICEArguments = 0 ;
75+ unsigned iceArguments = 0 ;
6376 ASTContext::GetBuiltinTypeError error;
64- getContext ().GetBuiltinType (builtinID, error, &ICEArguments );
77+ getContext ().GetBuiltinType (builtinID, error, &iceArguments );
6578 assert (error == ASTContext::GE_None && " Should not codegen an error" );
6679
67- for (auto [idx, _ ] : llvm::enumerate (e->arguments ())) {
68- ops.push_back (emitScalarOrConstFoldImmArg (ICEArguments , idx, e ));
80+ for (auto [idx, arg ] : llvm::enumerate (e->arguments ())) {
81+ ops.push_back (emitScalarOrConstFoldImmArg (iceArguments , idx, arg ));
6982 }
7083
7184 switch (builtinID) {
7285 default :
7386 return {};
74- case X86::BI_mm_prefetch: {
75- mlir::Value address = builder.createPtrBitcast (ops[0 ], voidTy);
76-
77- int64_t hint = getIntValueFromConstOp (ops[1 ]);
78- mlir::Value rw =
79- cir::ConstantOp::create (builder, getLoc (e->getExprLoc ()),
80- cir::IntAttr::get (sInt32Ty , (hint >> 2 ) & 0x1 ));
81- mlir::Value locality =
82- cir::ConstantOp::create (builder, getLoc (e->getExprLoc ()),
83- cir::IntAttr::get (sInt32Ty , hint & 0x3 ));
84- mlir::Value data = cir::ConstantOp::create (builder, getLoc (e->getExprLoc ()),
85- cir::IntAttr::get (sInt32Ty , 1 ));
86- mlir::Type voidTy = cir::VoidType::get (&getMLIRContext ());
87-
88- return cir::LLVMIntrinsicCallOp::create (
89- builder, getLoc (e->getExprLoc ()),
90- builder.getStringAttr (" prefetch" ), voidTy,
91- mlir::ValueRange{address, rw, locality, data})
92- .getResult ();
93- }
94- case X86::BI_mm_clflush: {
95- mlir::Type voidTy = cir::VoidType::get (&getMLIRContext ());
96- return cir::LLVMIntrinsicCallOp::create (
97- builder, getLoc (e->getExprLoc ()),
98- builder.getStringAttr (" x86.sse2.clflush" ), voidTy, ops[0 ])
99- .getResult ();
100- }
101- case X86::BI_mm_lfence: {
102- mlir::Type voidTy = cir::VoidType::get (&getMLIRContext ());
103- return cir::LLVMIntrinsicCallOp::create (
104- builder, getLoc (e->getExprLoc ()),
105- builder.getStringAttr (" x86.sse2.lfence" ), voidTy)
106- .getResult ();
107- }
108- case X86::BI_mm_pause: {
109- mlir::Type voidTy = cir::VoidType::get (&getMLIRContext ());
110- return cir::LLVMIntrinsicCallOp::create (
111- builder, getLoc (e->getExprLoc ()),
112- builder.getStringAttr (" x86.sse2.pause" ), voidTy)
113- .getResult ();
114- }
115- case X86::BI_mm_mfence: {
116- mlir::Type voidTy = cir::VoidType::get (&getMLIRContext ());
117- return cir::LLVMIntrinsicCallOp::create (
118- builder, getLoc (e->getExprLoc ()),
119- builder.getStringAttr (" x86.sse2.mfence" ), voidTy)
120- .getResult ();
121- }
122- case X86::BI_mm_sfence: {
123- mlir::Type voidTy = cir::VoidType::get (&getMLIRContext ());
124- return cir::LLVMIntrinsicCallOp::create (
125- builder, getLoc (e->getExprLoc ()),
126- builder.getStringAttr (" x86.sse.sfence" ), voidTy)
127- .getResult ();
128- }
87+ case X86::BI_mm_prefetch:
88+ return emitPrefetch (*this , e, ops[0 ], getIntValueFromConstOp (ops[1 ]));
89+ case X86::BI_mm_clflush:
90+ return emitIntrinsicCallOp (*this , e, " x86.sse2.clflush" , voidTy, ops[0 ]);
91+ case X86::BI_mm_lfence:
92+ return emitIntrinsicCallOp (*this , e, " x86.sse2.lfence" , voidTy);
93+ case X86::BI_mm_pause:
94+ return emitIntrinsicCallOp (*this , e, " x86.sse2.pause" , voidTy);
95+ case X86::BI_mm_mfence:
96+ return emitIntrinsicCallOp (*this , e, " x86.sse2.mfence" , voidTy);
97+ case X86::BI_mm_sfence:
98+ return emitIntrinsicCallOp (*this , e, " x86.sse.sfence" , voidTy);
12999 case X86::BI__rdtsc:
130100 case X86::BI__builtin_ia32_rdtscp:
131101 case X86::BI__builtin_ia32_lzcnt_u16:
@@ -134,7 +104,10 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID,
134104 case X86::BI__builtin_ia32_tzcnt_u16:
135105 case X86::BI__builtin_ia32_tzcnt_u32:
136106 case X86::BI__builtin_ia32_tzcnt_u64:
137- UNIMPLEMENTED_BUILTIN ();
107+ cgm.errorNYI (e->getSourceRange (),
108+ std::string (" unimplemented X86 builtin call: " ) +
109+ getContext ().BuiltinInfo .getName (builtinID));
110+ return {};
138111 case X86::BI__builtin_ia32_undef128:
139112 case X86::BI__builtin_ia32_undef256:
140113 case X86::BI__builtin_ia32_undef512:
@@ -181,7 +154,10 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID,
181154 case X86::BI__builtin_ia32_vec_set_v16hi:
182155 case X86::BI__builtin_ia32_vec_set_v8si:
183156 case X86::BI__builtin_ia32_vec_set_v4di:
184- UNIMPLEMENTED_BUILTIN ();
157+ cgm.errorNYI (e->getSourceRange (),
158+ std::string (" unimplemented X86 builtin call: " ) +
159+ getContext ().BuiltinInfo .getName (builtinID));
160+ return {};
185161 case X86::BI_mm_setcsr:
186162 case X86::BI__builtin_ia32_ldmxcsr: {
187163 Address tmp =
@@ -524,7 +500,10 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID,
524500 case X86::BI__builtin_ia32_pblendw256:
525501 case X86::BI__builtin_ia32_pblendd128:
526502 case X86::BI__builtin_ia32_pblendd256:
527- UNIMPLEMENTED_BUILTIN ();
503+ cgm.errorNYI (e->getSourceRange (),
504+ std::string (" unimplemented X86 builtin call: " ) +
505+ getContext ().BuiltinInfo .getName (builtinID));
506+ return {};
528507 case X86::BI__builtin_ia32_pshuflw:
529508 case X86::BI__builtin_ia32_pshuflw256:
530509 case X86::BI__builtin_ia32_pshuflw512: {
@@ -582,7 +561,10 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID,
582561 case X86::BI__builtin_ia32_vpermilps256:
583562 case X86::BI__builtin_ia32_vpermilpd512:
584563 case X86::BI__builtin_ia32_vpermilps512:
585- UNIMPLEMENTED_BUILTIN ();
564+ cgm.errorNYI (e->getSourceRange (),
565+ std::string (" unimplemented X86 builtin call: " ) +
566+ getContext ().BuiltinInfo .getName (builtinID));
567+ return {};
586568 case X86::BI__builtin_ia32_shufpd:
587569 case X86::BI__builtin_ia32_shufpd256:
588570 case X86::BI__builtin_ia32_shufpd512:
@@ -982,6 +964,9 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID,
982964 case X86::BI__builtin_ia32_vfcmaddcsh_round_mask3:
983965 case X86::BI__builtin_ia32_vfmaddcsh_round_mask3:
984966 case X86::BI__builtin_ia32_prefetchi:
985- UNIMPLEMENTED_BUILTIN ();
967+ cgm.errorNYI (e->getSourceRange (),
968+ std::string (" unimplemented X86 builtin call: " ) +
969+ getContext ().BuiltinInfo .getName (builtinID));
970+ return {};
986971 }
987972}
0 commit comments