Skip to content

Commit ef39145

Browse files
committed
[CIR] [Lowering] [X86_64] Support VAArg in shape
1 parent a18a580 commit ef39145

File tree

16 files changed

+624
-112
lines changed

16 files changed

+624
-112
lines changed

clang/include/clang/CIR/ABIArgInfo.h

+2
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ class ABIArgInfo {
252252
bool isExpand() const { return TheKind == Expand; }
253253
bool isCoerceAndExpand() const { return TheKind == CoerceAndExpand; }
254254

255+
bool isIgnore() const { return TheKind == Ignore; }
256+
255257
bool isSignExt() const {
256258
assert(isExtend() && "Invalid kind!");
257259
return SignExt;

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

+11
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
4848
return create<cir::ConstantOp>(loc, ty, getAttr<cir::IntAttr>(ty, val));
4949
}
5050

51+
mlir::Value getSignedInt(mlir::Location loc, int64_t val, unsigned numBits) {
52+
return getConstAPSInt(
53+
loc, llvm::APSInt(llvm::APInt(numBits, val), /*isUnsigned=*/false));
54+
}
55+
56+
mlir::Value getUnsignedInt(mlir::Location loc, uint64_t val,
57+
unsigned numBits) {
58+
return getConstAPSInt(
59+
loc, llvm::APSInt(llvm::APInt(numBits, val), /*isUnsigned=*/true));
60+
}
61+
5162
mlir::Value getConstAPInt(mlir::Location loc, mlir::Type typ,
5263
const llvm::APInt &val) {
5364
return create<cir::ConstantOp>(loc, typ, getAttr<cir::IntAttr>(typ, val));

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

+20
Original file line numberDiff line numberDiff line change
@@ -4485,6 +4485,26 @@ def AssumeSepStorageOp : CIR_Op<"assume.separate_storage", [SameTypeOperands]> {
44854485
}];
44864486
}
44874487

4488+
//===----------------------------------------------------------------------===//
4489+
// PtrMask Operations
4490+
//===----------------------------------------------------------------------===//
4491+
4492+
def PtrMaskOp : CIR_Op<"ptr_mask", [AllTypesMatch<["ptr", "result"]>]> {
4493+
let summary = "Masks out bits of the pointer according to a mask";
4494+
let description = [{
4495+
The `cir.ptr_mask` operation takes a pointer and an interger `mask` as its
4496+
argument and return the masked pointer type according to the `mask`.
4497+
}];
4498+
4499+
let arguments = (ins CIR_PointerType:$ptr,
4500+
CIR_IntType:$mask);
4501+
let results = (outs CIR_PointerType:$result);
4502+
4503+
let assemblyFormat = [{
4504+
`(` $ptr `,` $mask `:` type($mask) `)` `:` qualified(type($result)) attr-dict
4505+
}];
4506+
}
4507+
44884508
//===----------------------------------------------------------------------===//
44894509
// Branch Probability Operations
44904510
//===----------------------------------------------------------------------===//

clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,16 @@ struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> {
124124

125125
void setASTContext(clang::ASTContext *c) {
126126
astCtx = c;
127-
auto abiStr = c->getTargetInfo().getABI();
127+
const clang::TargetInfo &target = c->getTargetInfo();
128+
auto abiStr = target.getABI();
128129
switch (c->getCXXABIKind()) {
129130
case clang::TargetCXXABI::GenericItanium:
131+
if (target.getTriple().getArch() == llvm::Triple::x86_64) {
132+
cxxABI.reset(
133+
cir::LoweringPrepareCXXABI::createX86ABI(/*is64bit=*/true));
134+
break;
135+
}
136+
130137
cxxABI.reset(cir::LoweringPrepareCXXABI::createItaniumABI());
131138
break;
132139
case clang::TargetCXXABI::GenericAArch64:

clang/lib/CIR/Dialect/Transforms/LoweringPrepareCXXABI.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class LoweringPrepareCXXABI {
2828
public:
2929
static LoweringPrepareCXXABI *createItaniumABI();
3030
static LoweringPrepareCXXABI *createAArch64ABI(cir::AArch64ABIKind k);
31+
static LoweringPrepareCXXABI *createX86ABI(bool is64Bit);
3132

3233
virtual mlir::Value lowerVAArg(CIRBaseBuilderTy &builder, cir::VAArgOp op,
3334
const cir::CIRDataLayout &datalayout) = 0;

clang/lib/CIR/Dialect/Transforms/LoweringPrepareX86ABI.h

Whitespace-only changes.

clang/lib/CIR/Dialect/Transforms/TargetLowering/ABIInfoImpl.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ bool isAggregateTypeForABI(mlir::Type T) {
3636
return !LowerFunction::hasScalarEvaluationKind(T);
3737
}
3838

39+
mlir::Value emitRoundPointerUpToAlignment(cir::CIRBaseBuilderTy &builder,
40+
mlir::Value ptr, unsigned alignment) {
41+
// OverflowArgArea = (OverflowArgArea + Align - 1) & -Align;
42+
mlir::Location loc = ptr.getLoc();
43+
mlir::Value roundUp = builder.createPtrStride(
44+
loc, builder.createPtrBitcast(ptr, builder.getUIntNTy(8)),
45+
builder.getUnsignedInt(loc, alignment - 1, /*width=*/32));
46+
return builder.create<cir::PtrMaskOp>(
47+
loc, roundUp.getType(), roundUp,
48+
builder.getSignedInt(loc, -alignment, /*width=*/32));
49+
}
50+
3951
mlir::Type useFirstFieldIfTransparentUnion(mlir::Type Ty) {
4052
if (auto RT = mlir::dyn_cast<StructType>(Ty)) {
4153
if (RT.isUnion())

clang/lib/CIR/Dialect/Transforms/TargetLowering/ABIInfoImpl.h

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ bool classifyReturnType(const CIRCXXABI &CXXABI, LowerFunctionInfo &FI,
2525

2626
bool isAggregateTypeForABI(mlir::Type T);
2727

28+
mlir::Value emitRoundPointerUpToAlignment(cir::CIRBaseBuilderTy &builder,
29+
mlir::Value ptr, unsigned alignment);
30+
2831
/// Pass transparent unions as if they were the type of the first element. Sema
2932
/// should ensure that all elements of the union have the same "machine type".
3033
mlir::Type useFirstFieldIfTransparentUnion(mlir::Type Ty);

clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRCXXABI.h

-19
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,4 @@ CIRCXXABI *CreateItaniumCXXABI(LowerModule &CGM);
6666

6767
} // namespace cir
6868

69-
// FIXME(cir): Merge this into the CIRCXXABI class above. To do so, this code
70-
// should be updated to follow some level of codegen parity.
71-
namespace cir {
72-
73-
class LoweringPrepareCXXABI {
74-
public:
75-
static LoweringPrepareCXXABI *createItaniumABI();
76-
static LoweringPrepareCXXABI *createAArch64ABI(cir::AArch64ABIKind k);
77-
78-
virtual mlir::Value lowerVAArg(CIRBaseBuilderTy &builder, cir::VAArgOp op,
79-
const cir::CIRDataLayout &datalayout) = 0;
80-
virtual ~LoweringPrepareCXXABI() {}
81-
82-
virtual mlir::Value lowerDynamicCast(CIRBaseBuilderTy &builder,
83-
clang::ASTContext &astCtx,
84-
cir::DynamicCastOp op) = 0;
85-
};
86-
} // namespace cir
87-
8869
#endif // LLVM_CLANG_LIB_CIR_DIALECT_TRANSFORMS_TARGETLOWERING_CIRCXXABI_H

clang/lib/CIR/Dialect/Transforms/TargetLowering/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ add_clang_library(TargetLowering
1717
Targets/X86.cpp
1818
Targets/LoweringPrepareAArch64CXXABI.cpp
1919
Targets/LoweringPrepareItaniumCXXABI.cpp
20+
Targets/LoweringPrepareX86CXXABI.cpp
2021

2122
DEPENDS
2223
clangBasic

clang/lib/CIR/Dialect/Transforms/TargetLowering/ItaniumCXXABI.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
//
2121
//===----------------------------------------------------------------------===//
2222

23+
#include "../LoweringPrepareCXXABI.h"
2324
#include "CIRCXXABI.h"
2425
#include "LowerModule.h"
2526
#include "llvm/Support/ErrorHandling.h"

0 commit comments

Comments
 (0)