Skip to content
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

[CIR][HIP] Compile host code #1319

Merged
merged 1 commit into from
Feb 7, 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
10 changes: 4 additions & 6 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,8 @@ void CIRGenModule::emitGlobal(GlobalDecl GD) {
assert(!Global->hasAttr<IFuncAttr>() && "NYI");
assert(!Global->hasAttr<CPUDispatchAttr>() && "NYI");

if (langOpts.CUDA) {
if (langOpts.CUDA || langOpts.HIP) {
// clang uses the same flag when building HIP code
if (langOpts.CUDAIsDevice) {
// This will implicitly mark templates and their
// specializations as __host__ __device__.
Expand Down Expand Up @@ -3217,8 +3218,7 @@ void CIRGenModule::Release() {
if (astContext.getTargetInfo().getTriple().isWasm())
llvm_unreachable("NYI");

if (getTriple().isAMDGPU() ||
(getTriple().isSPIRV() && getTriple().getVendor() == llvm::Triple::AMD)) {
if (getTriple().isSPIRV() && getTriple().getVendor() == llvm::Triple::AMD) {
llvm_unreachable("NYI");
}

Expand All @@ -3229,9 +3229,7 @@ void CIRGenModule::Release() {
if (!astContext.CUDAExternalDeviceDeclODRUsedByHost.empty()) {
llvm_unreachable("NYI");
}
if (langOpts.HIP && !getLangOpts().OffloadingNewDriver) {
llvm_unreachable("NYI");
}

assert(!MissingFeatures::emitLLVMUsed());
assert(!MissingFeatures::sanStats());

Expand Down
56 changes: 56 additions & 0 deletions clang/lib/CIR/CodeGen/TargetInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,30 @@ class NVPTXTargetCIRGenInfo : public TargetCIRGenInfo {

} // namespace

//===----------------------------------------------------------------------===//
// AMDGPU ABI Implementation
//===----------------------------------------------------------------------===//

namespace {

class AMDGPUABIInfo : public ABIInfo {
public:
AMDGPUABIInfo(CIRGenTypes &cgt) : ABIInfo(cgt) {}

cir::ABIArgInfo classifyReturnType(QualType retTy) const;
cir::ABIArgInfo classifyArgumentType(QualType ty) const;

void computeInfo(CIRGenFunctionInfo &fnInfo) const override;
};

class AMDGPUTargetCIRGenInfo : public TargetCIRGenInfo {
public:
AMDGPUTargetCIRGenInfo(CIRGenTypes &cgt)
: TargetCIRGenInfo(std::make_unique<AMDGPUABIInfo>(cgt)) {}
};

} // namespace

// TODO(cir): remove the attribute once this gets used.
LLVM_ATTRIBUTE_UNUSED
static bool classifyReturnType(const CIRGenCXXABI &CXXABI,
Expand Down Expand Up @@ -495,6 +519,34 @@ void NVPTXABIInfo::computeInfo(CIRGenFunctionInfo &fnInfo) const {
fnInfo.getReturnInfo() = cir::ABIArgInfo::getDirect(CGT.convertType(retTy));
}

// Skeleton only. Implement when used in TargetLower stage.
cir::ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType retTy) const {
llvm_unreachable("not yet implemented");
}

cir::ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType ty) const {
llvm_unreachable("not yet implemented");
}

void AMDGPUABIInfo::computeInfo(CIRGenFunctionInfo &fnInfo) const {
// Top level CIR has unlimited arguments and return types. Lowering for ABI
// specific concerns should happen during a lowering phase. Assume everything
// is direct for now.
for (CIRGenFunctionInfo::arg_iterator it = fnInfo.arg_begin(),
ie = fnInfo.arg_end();
it != ie; ++it) {
if (testIfIsVoidTy(it->type))
it->info = cir::ABIArgInfo::getIgnore();
else
it->info = cir::ABIArgInfo::getDirect(CGT.convertType(it->type));
}
auto retTy = fnInfo.getReturnType();
if (testIfIsVoidTy(retTy))
fnInfo.getReturnInfo() = cir::ABIArgInfo::getIgnore();
else
fnInfo.getReturnInfo() = cir::ABIArgInfo::getDirect(CGT.convertType(retTy));
}

ABIInfo::~ABIInfo() {}

bool ABIInfo::isPromotableIntegerTypeForABI(QualType Ty) const {
Expand Down Expand Up @@ -690,5 +742,9 @@ const TargetCIRGenInfo &CIRGenModule::getTargetCIRGenInfo() {
case llvm::Triple::nvptx64: {
return SetCIRGenInfo(new NVPTXTargetCIRGenInfo(genTypes));
}

case llvm::Triple::amdgcn: {
return SetCIRGenInfo(new AMDGPUTargetCIRGenInfo(genTypes));
}
}
}
16 changes: 16 additions & 0 deletions clang/test/CIR/CodeGen/HIP/simple.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "../Inputs/cuda.h"

// RUN: %clang_cc1 -triple=amdgcn-amd-amdhsa -x hip -fclangir \
// RUN: -emit-cir %s -o %t.cir
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s


// This should emit as a normal C++ function.
__host__ void host_fn(int *a, int *b, int *c) {}

// CIR: cir.func @_Z7host_fnPiS_S_

// This shouldn't emit.
__device__ void device_fn(int* a, double b, float c) {}

// CHECK-NOT: cir.func @_Z9device_fnPidf