Skip to content

[CIR][CIRGen] Move CIRGenModule::getTargetCIRGenInfo() to CIRGenModule.cpp #1426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,56 @@ void CIRGenModule::setDSOLocal(CIRGlobalValueInterface gv) const {
gv.setDSOLocal(shouldAssumeDSOLocal(*this, gv));
}

const TargetCIRGenInfo &CIRGenModule::getTargetCIRGenInfo() {
if (theTargetCIRGenInfo)
return *theTargetCIRGenInfo;

const llvm::Triple &triple = getTarget().getTriple();

switch (triple.getArch()) {
default:
assert(false && "Target not yet supported!");

case llvm::Triple::aarch64_be:
case llvm::Triple::aarch64: {
AArch64ABIKind kind = AArch64ABIKind::AAPCS;
assert(getTarget().getABI() == "aapcs" ||
getTarget().getABI() == "darwinpcs" &&
"Only Darwin supported for aarch64");
kind = AArch64ABIKind::DarwinPCS;
return *(theTargetCIRGenInfo =
createAArch64TargetCIRGenInfo(genTypes, kind));
}

case llvm::Triple::x86_64: {
StringRef abi = getTarget().getABI();
X86AVXABILevel avxLevel = (abi == "avx512" ? X86AVXABILevel::AVX512
: abi == "avx" ? X86AVXABILevel::AVX
: X86AVXABILevel::None);

switch (triple.getOS()) {
default:
assert(false && "OSType NYI");
case llvm::Triple::Linux:
return *(theTargetCIRGenInfo =
createX86_64TargetCIRGenInfo(genTypes, avxLevel));
}
}

case llvm::Triple::spirv64: {
return *(theTargetCIRGenInfo = createSPIRVTargetCIRGenInfo(genTypes));
}

case llvm::Triple::nvptx64: {
return *(theTargetCIRGenInfo = createNVPTXTargetCIRGenInfo(genTypes));
}

case llvm::Triple::amdgcn: {
return *(theTargetCIRGenInfo = createAMDGPUTargetCIRGenInfo(genTypes));
}
}
}

const ABIInfo &CIRGenModule::getABIInfo() {
return getTargetCIRGenInfo().getABIInfo();
}
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/CodeGen/CIRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class CIRGenModule : public CIRGenTypeCache {
const std::string &getModuleNameHash() const { return ModuleNameHash; }

private:
mutable std::unique_ptr<TargetCIRGenInfo> TheTargetCIRGenInfo;
mutable std::unique_ptr<TargetCIRGenInfo> theTargetCIRGenInfo;

/// The builder is a helper class to create IR inside a function. The
/// builder is stateful, in particular it keeps an "insertion point": this
Expand Down
89 changes: 30 additions & 59 deletions clang/lib/CIR/CodeGen/TargetInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,9 @@ class DefaultABIInfo : public ABIInfo {
//===----------------------------------------------------------------------===//

namespace {
using ABIKind = cir::AArch64ABIKind;

class AArch64ABIInfo : public ABIInfo {
public:
enum ABIKind { AAPCS = 0, DarwinPCS, Win64 };

private:
ABIKind Kind;

Expand All @@ -113,7 +111,7 @@ class AArch64ABIInfo : public ABIInfo {

private:
ABIKind getABIKind() const { return Kind; }
bool isDarwinPCS() const { return Kind == DarwinPCS; }
bool isDarwinPCS() const { return Kind == ABIKind::DarwinPCS; }

cir::ABIArgInfo classifyReturnType(QualType RetTy, bool IsVariadic) const;
cir::ABIArgInfo classifyArgumentType(QualType RetTy, bool IsVariadic,
Expand Down Expand Up @@ -143,12 +141,18 @@ class AArch64ABIInfo : public ABIInfo {

class AArch64TargetCIRGenInfo : public TargetCIRGenInfo {
public:
AArch64TargetCIRGenInfo(CIRGenTypes &CGT, AArch64ABIInfo::ABIKind Kind)
AArch64TargetCIRGenInfo(CIRGenTypes &CGT, ABIKind Kind)
: TargetCIRGenInfo(std::make_unique<AArch64ABIInfo>(CGT, Kind)) {}
};

} // namespace

std::unique_ptr<TargetCIRGenInfo>
clang::CIRGen::createAArch64TargetCIRGenInfo(CIRGenTypes &CGT,
cir::AArch64ABIKind Kind) {
return std::make_unique<AArch64TargetCIRGenInfo>(CGT, Kind);
}

//===----------------------------------------------------------------------===//
// X86 ABI Implementation
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -230,6 +234,12 @@ class X86_64TargetCIRGenInfo : public TargetCIRGenInfo {
};
} // namespace

std::unique_ptr<TargetCIRGenInfo>
clang::CIRGen::createX86_64TargetCIRGenInfo(CIRGenTypes &CGT,
X86AVXABILevel AVXLevel) {
return std::make_unique<X86_64TargetCIRGenInfo>(CGT, AVXLevel);
}

//===----------------------------------------------------------------------===//
// Base ABI and target codegen info implementation common between SPIR and
// SPIR-V.
Expand Down Expand Up @@ -309,6 +319,11 @@ class SPIRVTargetCIRGenInfo : public CommonSPIRTargetCIRGenInfo {

} // namespace

std::unique_ptr<TargetCIRGenInfo>
clang::CIRGen::createSPIRVTargetCIRGenInfo(CIRGenTypes &CGT) {
return std::make_unique<SPIRVTargetCIRGenInfo>(CGT);
}

//===----------------------------------------------------------------------===//
// NVPTX ABI Implementation
//===----------------------------------------------------------------------===//
Expand All @@ -333,6 +348,11 @@ class NVPTXTargetCIRGenInfo : public TargetCIRGenInfo {

} // namespace

std::unique_ptr<TargetCIRGenInfo>
clang::CIRGen::createNVPTXTargetCIRGenInfo(CIRGenTypes &CGT) {
return std::make_unique<NVPTXTargetCIRGenInfo>(CGT);
}

//===----------------------------------------------------------------------===//
// AMDGPU ABI Implementation
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -361,6 +381,11 @@ class AMDGPUTargetCIRGenInfo : public TargetCIRGenInfo {

} // namespace

std::unique_ptr<TargetCIRGenInfo>
clang::CIRGen::createAMDGPUTargetCIRGenInfo(CIRGenTypes &CGT) {
return std::make_unique<AMDGPUTargetCIRGenInfo>(CGT);
}

// TODO(cir): remove the attribute once this gets used.
LLVM_ATTRIBUTE_UNUSED
static bool classifyReturnType(const CIRGenCXXABI &CXXABI,
Expand Down Expand Up @@ -702,57 +727,3 @@ mlir::Value TargetCIRGenInfo::performAddrSpaceCast(
// Try to preserve the source's name to make IR more readable.
return CGF.getBuilder().createAddrSpaceCast(Src, DestTy);
}

const TargetCIRGenInfo &CIRGenModule::getTargetCIRGenInfo() {
if (TheTargetCIRGenInfo)
return *TheTargetCIRGenInfo;

// Helper to set the unique_ptr while still keeping the return value.
auto SetCIRGenInfo = [&](TargetCIRGenInfo *P) -> const TargetCIRGenInfo & {
this->TheTargetCIRGenInfo.reset(P);
return *P;
};

const llvm::Triple &Triple = getTarget().getTriple();

switch (Triple.getArch()) {
default:
assert(false && "Target not yet supported!");

case llvm::Triple::aarch64_be:
case llvm::Triple::aarch64: {
AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
assert(getTarget().getABI() == "aapcs" ||
getTarget().getABI() == "darwinpcs" &&
"Only Darwin supported for aarch64");
Kind = AArch64ABIInfo::DarwinPCS;
return SetCIRGenInfo(new AArch64TargetCIRGenInfo(genTypes, Kind));
}

case llvm::Triple::x86_64: {
StringRef ABI = getTarget().getABI();
X86AVXABILevel AVXLevel = (ABI == "avx512" ? X86AVXABILevel::AVX512
: ABI == "avx" ? X86AVXABILevel::AVX
: X86AVXABILevel::None);

switch (Triple.getOS()) {
default:
assert(false && "OSType NYI");
case llvm::Triple::Linux:
return SetCIRGenInfo(new X86_64TargetCIRGenInfo(genTypes, AVXLevel));
}
}

case llvm::Triple::spirv64: {
return SetCIRGenInfo(new SPIRVTargetCIRGenInfo(genTypes));
}

case llvm::Triple::nvptx64: {
return SetCIRGenInfo(new NVPTXTargetCIRGenInfo(genTypes));
}

case llvm::Triple::amdgcn: {
return SetCIRGenInfo(new AMDGPUTargetCIRGenInfo(genTypes));
}
}
}
15 changes: 15 additions & 0 deletions clang/lib/CIR/CodeGen/TargetInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include "CIRGenValue.h"
#include "mlir/IR/Types.h"
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
#include "clang/CIR/Target/AArch64.h"
#include "clang/CIR/Target/x86.h"

#include <memory>

Expand Down Expand Up @@ -122,8 +124,21 @@ class TargetCIRGenInfo {
virtual ~TargetCIRGenInfo() {}
};

std::unique_ptr<TargetCIRGenInfo>
createAArch64TargetCIRGenInfo(CIRGenTypes &CGT, cir::AArch64ABIKind Kind);

std::unique_ptr<TargetCIRGenInfo>
createX86_64TargetCIRGenInfo(CIRGenTypes &CGT, cir::X86AVXABILevel AVXLevel);

void computeSPIRKernelABIInfo(CIRGenModule &CGM, CIRGenFunctionInfo &FI);

std::unique_ptr<TargetCIRGenInfo> createSPIRVTargetCIRGenInfo(CIRGenTypes &CGT);

std::unique_ptr<TargetCIRGenInfo> createNVPTXTargetCIRGenInfo(CIRGenTypes &CGT);

std::unique_ptr<TargetCIRGenInfo>
createAMDGPUTargetCIRGenInfo(CIRGenTypes &CGT);

} // namespace clang::CIRGen

#endif
Loading