Skip to content

Commit

Permalink
[CIR] Make X86ArgClass an enum class (#1080)
Browse files Browse the repository at this point in the history
It's currently polluting the `cir` namespace with very generic symbols
like `Integer` and `Memory`, which is pretty confusing. `X86_64ABIInfo`
already has `Class` alias for `X86ArgClass`, so we can use that alias to
qualify all uses.
  • Loading branch information
smeenai authored Nov 7, 2024
1 parent 7619b20 commit 94b4d89
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
2 changes: 1 addition & 1 deletion clang/include/clang/CIR/Target/x86.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ enum class X86AVXABILevel {
};

// Possible argument classifications according to the x86 ABI documentation.
enum X86ArgClass {
enum class X86ArgClass {
Integer = 0,
SSE,
SSEUp,
Expand Down
50 changes: 27 additions & 23 deletions clang/lib/CIR/CodeGen/TargetInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,10 @@ ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,

// Check some invariants
// FIXME: Enforce these by construction.
assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
assert((Hi != Class::Memory || Lo == Class::Memory) &&
"Invalid memory classification.");
assert((Hi != Class::SSEUp || Lo == Class::SSE) &&
"Invalid SSEUp classification.");

neededInt = 0;
neededSSE = 0;
Expand All @@ -391,15 +393,15 @@ ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,

// AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next available
// register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 and %r9 is used.
case Integer:
case Class::Integer:
++neededInt;

// Pick an 8-byte type based on the preferred type.
ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);

// If we have a sign or zero extended integer, make sure to return Extend so
// that the parameter gets the right LLVM IR attributes.
if (Hi == NoClass && mlir::isa<mlir::cir::IntType>(ResType)) {
if (Hi == Class::NoClass && mlir::isa<mlir::cir::IntType>(ResType)) {
assert(!Ty->getAs<EnumType>() && "NYI");
if (Ty->isSignedIntegerOrEnumerationType() &&
isPromotableIntegerTypeForABI(Ty))
Expand All @@ -411,7 +413,7 @@ ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
// AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next available SSE
// register is used, the registers are taken in the order from %xmm0 to
// %xmm7.
case SSE: {
case Class::SSE: {
mlir::Type CIRType = CGT.ConvertType(Ty);
ResType = GetSSETypeAtOffset(CIRType, 0, Ty, 0);
++neededSSE;
Expand All @@ -423,7 +425,7 @@ ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
switch (Hi) {
default:
assert(false && "NYI");
case NoClass:
case Class::NoClass:
break;
}

Expand Down Expand Up @@ -453,23 +455,23 @@ void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, Class &Lo,
// shouldn't be passed in registers for example, so there is no chance they
// can straddle an eightbyte. Verify & simplify.

Lo = Hi = NoClass;
Lo = Hi = Class::NoClass;
Class &Current = OffsetBase < 64 ? Lo : Hi;
Current = Memory;
Current = Class::Memory;

if (const auto *BT = Ty->getAs<BuiltinType>()) {
BuiltinType::Kind k = BT->getKind();
if (k == BuiltinType::Void) {
Current = NoClass;
Current = Class::NoClass;
} else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
assert(false && "NYI");
Lo = Integer;
Hi = Integer;
Lo = Class::Integer;
Hi = Class::Integer;
} else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Current = Integer;
Current = Class::Integer;
} else if (k == BuiltinType::Float || k == BuiltinType::Double ||
k == BuiltinType::Float16) {
Current = SSE;
Current = Class::SSE;
} else if (k == BuiltinType::LongDouble) {
assert(false && "NYI");
} else
Expand All @@ -482,7 +484,7 @@ void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, Class &Lo,

assert(!Ty->getAs<EnumType>() && "Enums NYI");
if (Ty->hasPointerRepresentation()) {
Current = Integer;
Current = Class::Integer;
return;
}

Expand All @@ -508,27 +510,29 @@ ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy) const {
classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);

// Check some invariants.
assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
assert((Hi != Class::Memory || Lo == Class::Memory) &&
"Invalid memory classification.");
assert((Hi != Class::SSEUp || Lo == Class::SSE) &&
"Invalid SSEUp classification.");

mlir::Type ResType = nullptr;
assert(Lo == NoClass || Lo == Integer ||
Lo == SSE && "Only NoClass and Integer supported so far");
assert(Lo == Class::NoClass || Lo == Class::Integer ||
Lo == Class::SSE && "Only NoClass and Integer supported so far");

switch (Lo) {
case NoClass:
assert(Hi == NoClass && "Only NoClass supported so far for Hi");
case Class::NoClass:
assert(Hi == Class::NoClass && "Only NoClass supported so far for Hi");
return ABIArgInfo::getIgnore();

// AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next available
// register of the sequence %rax, %rdx is used.
case Integer:
case Class::Integer:
ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);

// If we have a sign or zero extended integer, make sure to return Extend so
// that the parameter gets the right LLVM IR attributes.
// TODO: extend the above consideration to MLIR
if (Hi == NoClass && mlir::isa<mlir::cir::IntType>(ResType)) {
if (Hi == Class::NoClass && mlir::isa<mlir::cir::IntType>(ResType)) {
// Treat an enum type as its underlying type.
if (const auto *EnumTy = RetTy->getAs<EnumType>())
RetTy = EnumTy->getDecl()->getIntegerType();
Expand All @@ -542,7 +546,7 @@ ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy) const {

// AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next available SSE
// register of the sequence %xmm0, %xmm1 is used.
case SSE:
case Class::SSE:
ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
break;

Expand Down

0 comments on commit 94b4d89

Please sign in to comment.