Skip to content

Commit 1faee59

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 6fb2a80 commit 1faee59

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
@@ -1854,6 +1854,40 @@ def FuncOp : CIR_Op<"func", [
18541854
let hasVerifier = 1;
18551855
}
18561856

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

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ struct MissingFeatures {
225225
static bool isMemcpyEquivalentSpecialMember() { return false; }
226226
static bool isTrivialCtorOrDtor() { return false; }
227227
static bool implicitConstructorArgs() { return false; }
228+
static bool intrinsics() { return false; }
229+
static bool attributeNoBuiltin() { return false; }
228230

229231
// Missing types
230232
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
@@ -39,6 +39,28 @@ mlir::Value CIRGenBuilderTy::getArrayElement(mlir::Location arrayLocBegin,
3939
return create<cir::PtrStrideOp>(arrayLocEnd, flatPtrTy, basePtr, idx);
4040
}
4141

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+
}
63+
4264
// This can't be defined in Address.h because that file is included by
4365
// CIRGenBuilder.h
4466
Address Address::withElementType(CIRGenBuilderTy &builder,

clang/lib/CIR/CodeGen/CIRGenBuilder.h

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

232+
cir::ConstantOp getConstInt(mlir::Location loc, llvm::APSInt intVal);
233+
234+
cir::ConstantOp getConstInt(mlir::Location loc, llvm::APInt intVal);
235+
236+
cir::ConstantOp getConstInt(mlir::Location loc, mlir::Type t, uint64_t c);
237+
238+
cir::ConstantOp getConstFP(mlir::Location loc, mlir::Type t,
239+
llvm::APFloat fpVal) {
240+
assert((mlir::isa<cir::SingleType, cir::DoubleType>(t)) &&
241+
"expected cir::SingleType or cir::DoubleType");
242+
return create<cir::ConstantOp>(loc, getAttr<cir::FPAttr>(t, fpVal));
243+
}
244+
232245
bool isInt8Ty(mlir::Type i) {
233246
return i == typeCache.UInt8Ty || i == typeCache.SInt8Ty;
234247
}

0 commit comments

Comments
 (0)