Skip to content

Commit bfccdb1

Browse files
Resolve conflicts with PR #167401 + code style
1 parent b6118d4 commit bfccdb1

File tree

7 files changed

+84
-92
lines changed

7 files changed

+84
-92
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2589,7 +2589,7 @@ def CIR_FuncOp : CIR_Op<"func", [
25892589
// LLVMIntrinsicCallOp
25902590
//===----------------------------------------------------------------------===//
25912591

2592-
def CIR_LLVMIntrinsicCallOp : CIR_Op<"llvm.intrinsic"> {
2592+
def CIR_LLVMIntrinsicCallOp : CIR_Op<"call_llvm_intrinsic"> {
25932593
let summary = "Call to llvm intrinsic functions that is not defined in CIR";
25942594
let description = [{
25952595
`cir.llvm.intrinsic` operation represents a call-like expression which has

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -626,17 +626,17 @@ CIRGenFunction::emitTargetBuiltinExpr(unsigned builtinID, const CallExpr *e,
626626
}
627627

628628
mlir::Value CIRGenFunction::emitScalarOrConstFoldImmArg(
629-
const unsigned ICEArguments, const unsigned idx, const CallExpr *e) {
629+
const unsigned iceArguments, const unsigned idx, const Expr *argExpr) {
630630
mlir::Value arg = {};
631-
if ((ICEArguments & (1 << idx)) == 0) {
632-
arg = emitScalarExpr(e->getArg(idx));
631+
if ((iceArguments & (1 << idx)) == 0) {
632+
arg = emitScalarExpr(argExpr);
633633
} else {
634634
// If this is required to be a constant, constant fold it so that we
635635
// know that the generated intrinsic gets a ConstantInt.
636636
const std::optional<llvm::APSInt> result =
637-
e->getArg(idx)->getIntegerConstantExpr(getContext());
637+
argExpr->getIntegerConstantExpr(getContext());
638638
assert(result && "Expected argument to be a constant");
639-
arg = builder.getConstInt(getLoc(e->getSourceRange()), *result);
639+
arg = builder.getConstInt(getLoc(argExpr->getSourceRange()), *result);
640640
}
641641
return arg;
642642
}

clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp

Lines changed: 62 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,35 @@
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

2920
using namespace clang;
3021
using 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

3750
mlir::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
}

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ class CIRGenFunction : public CIRGenTypeCache {
210210
return getEvaluationKind(type) == cir::TEK_Aggregate;
211211
}
212212

213+
/// Get integer from a mlir::Value that is an int constant or a constant op.
214+
static int64_t getIntValueFromConstOp(mlir::Value val) {
215+
auto constOp = val.getDefiningOp<cir::ConstantOp>();
216+
assert(constOp && "getIntValueFromConstOp call with non ConstantOp");
217+
return constOp.getIntValue().getSExtValue();
218+
}
219+
213220
CIRGenFunction(CIRGenModule &cgm, CIRGenBuilderTy &builder,
214221
bool suppressNewContext = false);
215222
~CIRGenFunction();
@@ -1699,8 +1706,8 @@ class CIRGenFunction : public CIRGenTypeCache {
16991706
void emitScalarInit(const clang::Expr *init, mlir::Location loc,
17001707
LValue lvalue, bool capturedByInit = false);
17011708

1702-
mlir::Value emitScalarOrConstFoldImmArg(unsigned ICEArguments, unsigned idx,
1703-
const CallExpr *e);
1709+
mlir::Value emitScalarOrConstFoldImmArg(unsigned iceArguments, unsigned idx,
1710+
const Expr *argExpr);
17041711

17051712
void emitStaticVarDecl(const VarDecl &d, cir::GlobalLinkageKind linkage);
17061713

clang/test/CIR/CodeGen/X86/pause.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ void test_mm_pause(void) {
1717
// CIR-LABEL: test_mm_pause
1818
// LLVM-LABEL: test_mm_pause
1919
_mm_pause();
20-
// CIR: {{%.*}} = cir.llvm.intrinsic "x86.sse2.pause" : () -> !void
20+
// CIR: {{%.*}} = cir.call_llvm_intrinsic "x86.sse2.pause" : () -> !void
2121
// LLVM: call void @llvm.x86.sse2.pause()
2222
}

clang/test/CIR/CodeGen/X86/sse-builtins.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ void test_mm_sfence(void) {
2828
// CIR-LABEL: test_mm_sfence
2929
// LLVM-LABEL: test_mm_sfence
3030
_mm_sfence();
31-
// CIR: {{%.*}} = cir.llvm.intrinsic "x86.sse.sfence" : () -> !void
31+
// CIR: {{%.*}} = cir.call_llvm_intrinsic "x86.sse.sfence" : () -> !void
3232
// LLVM: call void @llvm.x86.sse.sfence()
3333
}
3434

3535
void test_mm_setcsr(unsigned int A) {
3636
// CIR-LABEL: test_mm_setcsr
3737
// CIR: cir.store {{.*}}, {{.*}} : !u32i
38-
// CIR: cir.llvm.intrinsic "x86.sse.ldmxcsr" {{.*}} : (!cir.ptr<!u32i>) -> !void
38+
// CIR: cir.call_llvm_intrinsic "x86.sse.ldmxcsr" {{.*}} : (!cir.ptr<!u32i>) -> !void
3939

4040
// LLVM-LABEL: test_mm_setcsr
4141
// LLVM: store i32
@@ -45,7 +45,7 @@ void test_mm_setcsr(unsigned int A) {
4545

4646
unsigned int test_mm_getcsr(void) {
4747
// CIR-LABEL: test_mm_getcsr
48-
// CIR: cir.llvm.intrinsic "x86.sse.stmxcsr" %{{.*}} : (!cir.ptr<!u32i>) -> !void
48+
// CIR: cir.call_llvm_intrinsic "x86.sse.stmxcsr" %{{.*}} : (!cir.ptr<!u32i>) -> !void
4949
// CIR: cir.load {{.*}} : !cir.ptr<!u32i>, !u32i
5050

5151
// LLVM-LABEL: test_mm_getcsr

clang/test/CIR/CodeGen/X86/sse2-builtins.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void test_mm_clflush(void* A) {
2323
// CIR-LABEL: test_mm_clflush
2424
// LLVM-LABEL: test_mm_clflush
2525
_mm_clflush(A);
26-
// CIR-CHECK: {{%.*}} = cir.llvm.intrinsic "x86.sse2.clflush" {{%.*}} : (!cir.ptr<!void>) -> !void
26+
// CIR-CHECK: {{%.*}} = cir.call_llvm_intrinsic "x86.sse2.clflush" {{%.*}} : (!cir.ptr<!void>) -> !void
2727
// LLVM-CHECK: call void @llvm.x86.sse2.clflush(ptr {{%.*}})
2828
}
2929

@@ -69,15 +69,15 @@ void test_mm_lfence(void) {
6969
// CIR-CHECK-LABEL: test_mm_lfence
7070
// LLVM-CHECK-LABEL: test_mm_lfence
7171
_mm_lfence();
72-
// CIR-CHECK: {{%.*}} = cir.llvm.intrinsic "x86.sse2.lfence" : () -> !void
72+
// CIR-CHECK: {{%.*}} = cir.call_llvm_intrinsic "x86.sse2.lfence" : () -> !void
7373
// LLVM-CHECK: call void @llvm.x86.sse2.lfence()
7474
}
7575

7676
void test_mm_mfence(void) {
7777
// CIR-CHECK-LABEL: test_mm_mfence
7878
// LLVM-CHECK-LABEL: test_mm_mfence
7979
_mm_mfence();
80-
// CIR-CHECK: {{%.*}} = cir.llvm.intrinsic "x86.sse2.mfence" : () -> !void
80+
// CIR-CHECK: {{%.*}} = cir.call_llvm_intrinsic "x86.sse2.mfence" : () -> !void
8181
// LLVM-CHECK: call void @llvm.x86.sse2.mfence()
8282
}
8383

0 commit comments

Comments
 (0)