Skip to content

Commit 2492cdc

Browse files
Merge remote-tracking branch 'origin/main' into cir-switch
2 parents ca883d9 + 6ce3969 commit 2492cdc

37 files changed

+1162
-139
lines changed

clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
8989
return cir::IntType::get(getContext(), N, false);
9090
}
9191

92+
static unsigned getCIRIntOrFloatBitWidth(mlir::Type eltTy) {
93+
if (auto intType = mlir::dyn_cast<cir::IntTypeInterface>(eltTy))
94+
return intType.getWidth();
95+
if (auto floatType = mlir::dyn_cast<cir::FPTypeInterface>(eltTy))
96+
return floatType.getWidth();
97+
98+
llvm_unreachable("Wrong type passed in or Non-CIR type passed in");
99+
}
92100
cir::IntType getSIntNTy(int N) {
93101
return cir::IntType::get(getContext(), N, true);
94102
}
@@ -188,6 +196,16 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
188196
return cir::CmpOp::create(*this, loc, getBoolTy(), kind, lhs, rhs);
189197
}
190198

199+
cir::VecCmpOp createVecCompare(mlir::Location loc, cir::CmpOpKind kind,
200+
mlir::Value lhs, mlir::Value rhs) {
201+
VectorType vecCast = mlir::cast<VectorType>(lhs.getType());
202+
auto integralTy =
203+
getSIntNTy(getCIRIntOrFloatBitWidth(vecCast.getElementType()));
204+
VectorType integralVecTy =
205+
VectorType::get(context, integralTy, vecCast.getSize());
206+
return cir::VecCmpOp::create(*this, loc, integralVecTy, kind, lhs, rhs);
207+
}
208+
191209
mlir::Value createIsNaN(mlir::Location loc, mlir::Value operand) {
192210
return createCompare(loc, cir::CmpOpKind::ne, operand, operand);
193211
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,29 @@ def CIR_AnyIntOrFloatAttr : AnyAttrOf<[CIR_AnyIntAttr, CIR_AnyFPAttr],
3838
string cppType = "::mlir::TypedAttr";
3939
}
4040

41+
//===----------------------------------------------------------------------===//
42+
// GlobalViewAttr constraints
43+
//===----------------------------------------------------------------------===//
44+
45+
def CIR_AnyGlobalViewAttr : CIR_AttrConstraint<"::cir::GlobalViewAttr", "GlobalView attribute">;
46+
47+
def CIR_AnyIntOrGlobalViewAttr : AnyAttrOf<[CIR_AnyIntAttr, CIR_AnyGlobalViewAttr],
48+
"integer or global view attribute"> {
49+
string cppType = "::mlir::TypedAttr";
50+
}
51+
4152
//===----------------------------------------------------------------------===//
4253
// ArrayAttr constraints
4354
//===----------------------------------------------------------------------===//
4455

4556
def CIR_IntArrayAttr : TypedArrayAttrBase<CIR_AnyIntAttr,
4657
"integer array attribute">;
4758

59+
def CIR_IntOrGlobalViewArrayAttr : TypedArrayAttrBase<CIR_AnyIntOrGlobalViewAttr,
60+
"integer or global view array attribute">{
61+
string cppType = "::mlir::ArrayAttr";
62+
}
63+
4864
//===----------------------------------------------------------------------===//
4965
// TBAAAttr constraints
5066
//===----------------------------------------------------------------------===//

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,8 +849,10 @@ def CIR_TypeInfoAttr : CIR_Attr<"TypeInfo", "typeinfo", [TypedAttrInterface]> {
849849
```
850850
}];
851851

852-
let parameters = (ins AttributeSelfTypeParameter<"">:$type,
853-
"mlir::ArrayAttr":$data);
852+
let parameters = (ins
853+
AttributeSelfTypeParameter<"">:$type,
854+
CIR_IntOrGlobalViewArrayAttr:$data
855+
);
854856

855857
let builders = [
856858
AttrBuilderWithInferredContext<(ins "mlir::Type":$type,

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6118,6 +6118,39 @@ def CIR_LinkerOptionsOp : CIR_Op<"linker_options", [
61186118
}];
61196119
}
61206120

6121+
//===----------------------------------------------------------------------===//
6122+
// BlockAddressOp
6123+
//===----------------------------------------------------------------------===//
6124+
6125+
def CIR_BlockAddressOp : CIR_Op<"blockaddress", [Pure]> {
6126+
let summary = "Get the address of a cir.label within a function";
6127+
let description = [{
6128+
The `cir.blockaddress` operation takes a function name and a label and
6129+
produces a pointer value that represents the address of that cir.label within
6130+
the specified function.
6131+
6132+
This operation models GCC's "labels as values" extension (`&&label`), which
6133+
allows taking the address of a local label and using it as a computed
6134+
jump target (e.g., with `goto *addr;`).
6135+
6136+
Example:
6137+
```mlir
6138+
%1 = cir.alloca !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>, ["ptr", init] {alignment = 8 : i64}
6139+
%addr = cir.blockaddress("foo", "label") -> !cir.ptr<!cir.void>
6140+
cir.store align(8) %addr, %1 : !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>
6141+
cir.br ^bb1
6142+
^bb1:
6143+
cir.label "label"
6144+
```
6145+
}];
6146+
6147+
let arguments = (ins FlatSymbolRefAttr:$func, StrAttr:$label);
6148+
let results = (outs CIR_VoidPtrType:$addr);
6149+
let assemblyFormat = [{
6150+
`(` $func `,` $label `)` `->` qualified(type($addr)) attr-dict
6151+
}];
6152+
}
6153+
61216154
//===----------------------------------------------------------------------===//
61226155
// Standard library function calls
61236156
//===----------------------------------------------------------------------===//

clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ let cppNamespace = "::cir" in {
2828
/*defaultImplementation=*/ [{
2929
return $_attr.getAst()->template hasAttr<clang::InitPriorityAttr>();
3030
}]
31+
>,
32+
33+
// Requires hasInitPriorityAttr before dereferencing
34+
InterfaceMethod<"", "const clang::InitPriorityAttr*", "getInitPriorityAttr", (ins), [{}],
35+
/*defaultImplementation=*/ [{
36+
return $_attr.getAst()->template getAttr<clang::InitPriorityAttr>();
37+
}]
3138
>
3239
];
3340
}

clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,44 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned BuiltinID,
306306
Ops.push_back(emitScalarOrConstFoldImmArg(ICEArguments, i, E));
307307
}
308308

309+
// OG has unordered comparison as a form of optimization in addition to
310+
// ordered comparison, while CIR doesn't.
311+
//
312+
// This means that we can't encode the comparison code of UGT (unordered
313+
// greater than), at least not at the CIR level.
314+
//
315+
// The boolean shouldInvert compensates for this.
316+
// For example: to get to the comparison code UGT, we pass in
317+
// getVectorFCmpIR(OLE, shouldInvert = true) since OLE is the inverse of UGT.
318+
319+
// There are several ways to support this otherwise:
320+
// - register extra CmpOpKind for unordered comparison types and build the
321+
// translation code for
322+
// to go from CIR -> LLVM dialect. Notice we get this naturally with
323+
// shouldInvert, benefiting from existing infrastructure, albeit having to
324+
// generate an extra `not` at CIR).
325+
// - Just add extra comparison code to a new VecCmpOpKind instead of
326+
// cluttering CmpOpKind.
327+
// - Add a boolean in VecCmpOp to indicate if it's doing unordered or ordered
328+
// comparison
329+
// - Just emit the intrinsics call instead of calling this helper, see how the
330+
// LLVM lowering handles this.
331+
auto getVectorFCmpIR = [this, &Ops, &E](cir::CmpOpKind pred,
332+
bool shouldInvert, bool isSignaling) {
333+
assert(!cir::MissingFeatures::CGFPOptionsRAII());
334+
auto loc = getLoc(E->getExprLoc());
335+
mlir::Value cmp;
336+
if (builder.getIsFPConstrained())
337+
// TODO: Add isSignaling boolean once emitConstrainedFPCall implemented
338+
assert(cir::MissingFeatures::emitConstrainedFPCall());
339+
else
340+
cmp = builder.createVecCompare(loc, pred, Ops[0], Ops[1]);
341+
342+
mlir::Value bitCast = builder.createBitcast(
343+
shouldInvert ? builder.createNot(cmp) : cmp, Ops[0].getType());
344+
return bitCast;
345+
};
346+
309347
switch (BuiltinID) {
310348
default:
311349
return nullptr;
@@ -1702,10 +1740,12 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned BuiltinID,
17021740
llvm_unreachable("cmpneqps NYI");
17031741
case X86::BI__builtin_ia32_cmpnltps:
17041742
case X86::BI__builtin_ia32_cmpnltpd:
1705-
llvm_unreachable("cmpnltps NYI");
1743+
return getVectorFCmpIR(cir::CmpOpKind::lt, /*shouldInvert=*/true,
1744+
/*isSignaling=*/true);
17061745
case X86::BI__builtin_ia32_cmpnleps:
17071746
case X86::BI__builtin_ia32_cmpnlepd:
1708-
llvm_unreachable("cmpnleps NYI");
1747+
return getVectorFCmpIR(cir::CmpOpKind::le, /*shouldInvert=*/true,
1748+
/*isSignaling=*/true);
17091749
case X86::BI__builtin_ia32_cmpordps:
17101750
case X86::BI__builtin_ia32_cmpordpd:
17111751
llvm_unreachable("cmpordps NYI");

clang/lib/CIR/CodeGen/CIRGenCUDANV.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ class CIRGenNVCUDARuntime : public CIRGenCUDARuntime {
7575

7676
mlir::Operation *getKernelHandle(cir::FuncOp fn, GlobalDecl GD) override;
7777

78+
mlir::Operation *getKernelStub(mlir::Operation *handle) override {
79+
auto loc = KernelStubs.find(handle);
80+
assert(loc != KernelStubs.end());
81+
return loc->second;
82+
}
83+
7884
void internalizeDeviceSideVar(const VarDecl *d,
7985
cir::GlobalLinkageKind &linkage) override;
8086
/// Returns function or variable name on device side even if the current

clang/lib/CIR/CodeGen/CIRGenCUDARuntime.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class CIRGenCUDARuntime {
4545
const CUDAKernelCallExpr *expr,
4646
ReturnValueSlot retValue);
4747
virtual mlir::Operation *getKernelHandle(cir::FuncOp fn, GlobalDecl GD) = 0;
48+
/// Get kernel stub by kernel handle.
49+
virtual mlir::Operation *getKernelStub(mlir::Operation *handle) = 0;
50+
4851
virtual void internalizeDeviceSideVar(const VarDecl *d,
4952
cir::GlobalLinkageKind &linkage) = 0;
5053
/// Returns function or variable name on device side even if the current

clang/lib/CIR/CodeGen/CIRGenCXXABI.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ class CIRGenCXXABI {
230230
/// this emits virtual table tables.
231231
virtual void emitVirtualInheritanceTables(const CXXRecordDecl *RD) = 0;
232232

233+
virtual bool exportThunk() = 0;
234+
virtual void setThunkLinkage(cir::FuncOp Thunk, bool ForVTable, GlobalDecl GD,
235+
bool ReturnAdjustment) = 0;
236+
233237
virtual mlir::Attribute getAddrOfRTTIDescriptor(mlir::Location loc,
234238
QualType Ty) = 0;
235239
virtual CatchTypeInfo

clang/lib/CIR/CodeGen/CIRGenCall.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,14 @@ const CIRGenFunctionInfo &CIRGenTypes::arrangeCXXMethodCall(
12961296
info, paramInfos, required);
12971297
}
12981298

1299+
const CIRGenFunctionInfo &
1300+
CIRGenTypes::arrangeUnprototypedMustTailThunk(const CXXMethodDecl *md) {
1301+
assert(md->isVirtual() && "only methods have thunks");
1302+
CanQual<FunctionProtoType> FTP = GetFormalType(md);
1303+
CanQualType ArgTys[] = {DeriveThisType(md->getParent(), md)};
1304+
return arrangeCIRFunctionInfo(astContext.VoidTy, cir::FnInfoOpts::None,
1305+
ArgTys, FTP->getExtInfo(), {}, RequiredArgs(1));
1306+
}
12991307
/// Figure out the rules for calling a function with the given formal type using
13001308
/// the given arguments. The arguments are necessary because the function might
13011309
/// be unprototyped, in which case it's target-dependent in crazy ways.

0 commit comments

Comments
 (0)