Skip to content

Commit dc85d27

Browse files
committed
[CIR] Upstream minimal builtin function call support
This patch adds all bits required to implement builtin function calls to ClangIR. It doesn't actually implement any of the builtins except those that fold to a constant ahead of CodeGen (__builtin_is_constant_evaluated() being one example). It also adds the LLVMIntrinsicCallOp instruction to the CIR dialect.
1 parent c140783 commit dc85d27

File tree

12 files changed

+679
-8
lines changed

12 files changed

+679
-8
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,6 +1858,40 @@ def FuncOp : CIR_Op<"func", [
18581858
let hasVerifier = 1;
18591859
}
18601860

1861+
//===----------------------------------------------------------------------===//
1862+
// LLVMIntrinsicCallOp
1863+
//===----------------------------------------------------------------------===//
1864+
1865+
def LLVMIntrinsicCallOp : CIR_Op<"llvm.intrinsic"> {
1866+
let summary = "Call to llvm intrinsic functions that is not defined in CIR";
1867+
let description = [{
1868+
The `cir.llvm.intrinsic` operation represents a call-like expression which has
1869+
a return type and arguments that map directly to an llvm intrinsic.
1870+
It only records its own name (`intrinsic_name`).
1871+
}];
1872+
1873+
let results = (outs Optional<CIR_AnyType>:$result);
1874+
let arguments = (ins
1875+
StrAttr:$intrinsic_name, Variadic<CIR_AnyType>:$arg_ops);
1876+
1877+
let skipDefaultBuilders = 1;
1878+
1879+
let assemblyFormat = [{
1880+
$intrinsic_name $arg_ops `:` functional-type($arg_ops, $result) attr-dict
1881+
}];
1882+
1883+
let builders = [
1884+
OpBuilder<(ins "mlir::StringAttr":$intrinsic_name, "mlir::Type":$resType,
1885+
CArg<"mlir::ValueRange", "{}">:$operands), [{
1886+
$_state.addAttribute("intrinsic_name", intrinsic_name);
1887+
$_state.addOperands(operands);
1888+
if (resType)
1889+
$_state.addTypes(resType);
1890+
}]>,
1891+
];
1892+
1893+
}
1894+
18611895
//===----------------------------------------------------------------------===//
18621896
// CallOp
18631897
//===----------------------------------------------------------------------===//

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ struct MissingFeatures {
216216
static bool peepholeProtection() { return false; }
217217
static bool instrumentation() { return false; }
218218
static bool cleanupAfterErrorDiags() { return false; }
219+
static bool intrinsics() { return false; }
220+
static bool attributeNoBuiltin() { return false; }
219221

220222
// Missing types
221223
static bool dataMemberType() { return false; }

clang/lib/CIR/CodeGen/CIRGenBuilder.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,25 @@ mlir::Value CIRGenBuilderTy::getArrayElement(mlir::Location arrayLocBegin,
3838
const mlir::Type flatPtrTy = basePtr.getType();
3939
return create<cir::PtrStrideOp>(arrayLocEnd, flatPtrTy, basePtr, idx);
4040
}
41+
42+
cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc,
43+
llvm::APSInt intVal) {
44+
bool isSigned = intVal.isSigned();
45+
auto width = intVal.getBitWidth();
46+
cir::IntType t = isSigned ? getSIntNTy(width) : getUIntNTy(width);
47+
return getConstInt(loc, t,
48+
isSigned ? intVal.getSExtValue() : intVal.getZExtValue());
49+
}
50+
51+
cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc,
52+
llvm::APInt intVal) {
53+
auto width = intVal.getBitWidth();
54+
cir::IntType t = getUIntNTy(width);
55+
return getConstInt(loc, t, intVal.getZExtValue());
56+
}
57+
58+
cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc, mlir::Type t,
59+
uint64_t c) {
60+
assert(mlir::isa<cir::IntType>(t) && "expected cir::IntType");
61+
return create<cir::ConstantOp>(loc, cir::IntAttr::get(t, c));
62+
}

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,19 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
201201
cir::IntType getUInt32Ty() { return typeCache.UInt32Ty; }
202202
cir::IntType getUInt64Ty() { return typeCache.UInt64Ty; }
203203

204+
cir::ConstantOp getConstInt(mlir::Location loc, llvm::APSInt intVal);
205+
206+
cir::ConstantOp getConstInt(mlir::Location loc, llvm::APInt intVal);
207+
208+
cir::ConstantOp getConstInt(mlir::Location loc, mlir::Type t, uint64_t c);
209+
210+
cir::ConstantOp getConstFP(mlir::Location loc, mlir::Type t,
211+
llvm::APFloat fpVal) {
212+
assert((mlir::isa<cir::SingleType, cir::DoubleType>(t)) &&
213+
"expected cir::SingleType or cir::DoubleType");
214+
return create<cir::ConstantOp>(loc, getAttr<cir::FPAttr>(t, fpVal));
215+
}
216+
204217
bool isInt8Ty(mlir::Type i) {
205218
return i == typeCache.UInt8Ty || i == typeCache.SInt8Ty;
206219
}

0 commit comments

Comments
 (0)