Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f6baa89

Browse files
committedFeb 7, 2025·
[HIP] Compile host code
1 parent a07dbdf commit f6baa89

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed
 

‎clang/lib/CIR/CodeGen/CIRGenModule.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,8 @@ void CIRGenModule::emitGlobal(GlobalDecl GD) {
515515
assert(!Global->hasAttr<IFuncAttr>() && "NYI");
516516
assert(!Global->hasAttr<CPUDispatchAttr>() && "NYI");
517517

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

3220-
if (getTriple().isAMDGPU() ||
3221-
(getTriple().isSPIRV() && getTriple().getVendor() == llvm::Triple::AMD)) {
3221+
if (getTriple().isSPIRV() && getTriple().getVendor() == llvm::Triple::AMD) {
32223222
llvm_unreachable("NYI");
32233223
}
32243224

@@ -3229,9 +3229,7 @@ void CIRGenModule::Release() {
32293229
if (!astContext.CUDAExternalDeviceDeclODRUsedByHost.empty()) {
32303230
llvm_unreachable("NYI");
32313231
}
3232-
if (langOpts.HIP && !getLangOpts().OffloadingNewDriver) {
3233-
llvm_unreachable("NYI");
3234-
}
3232+
32353233
assert(!MissingFeatures::emitLLVMUsed());
32363234
assert(!MissingFeatures::sanStats());
32373235

‎clang/lib/CIR/CodeGen/TargetInfo.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,30 @@ class NVPTXTargetCIRGenInfo : public TargetCIRGenInfo {
329329

330330
} // namespace
331331

332+
//===----------------------------------------------------------------------===//
333+
// AMDGPU ABI Implementation
334+
//===----------------------------------------------------------------------===//
335+
336+
namespace {
337+
338+
class AMDGPUABIInfo : public ABIInfo {
339+
public:
340+
AMDGPUABIInfo(CIRGenTypes &cgt) : ABIInfo(cgt) {}
341+
342+
cir::ABIArgInfo classifyReturnType(QualType retTy) const;
343+
cir::ABIArgInfo classifyArgumentType(QualType ty) const;
344+
345+
void computeInfo(CIRGenFunctionInfo &fnInfo) const override;
346+
};
347+
348+
class AMDGPUTargetCIRGenInfo : public TargetCIRGenInfo {
349+
public:
350+
AMDGPUTargetCIRGenInfo(CIRGenTypes &cgt)
351+
: TargetCIRGenInfo(std::make_unique<AMDGPUABIInfo>(cgt)) {}
352+
};
353+
354+
} // namespace
355+
332356
// TODO(cir): remove the attribute once this gets used.
333357
LLVM_ATTRIBUTE_UNUSED
334358
static bool classifyReturnType(const CIRGenCXXABI &CXXABI,
@@ -495,6 +519,34 @@ void NVPTXABIInfo::computeInfo(CIRGenFunctionInfo &fnInfo) const {
495519
fnInfo.getReturnInfo() = cir::ABIArgInfo::getDirect(CGT.convertType(retTy));
496520
}
497521

522+
// Skeleton only. Implement when used in TargetLower stage.
523+
cir::ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType retTy) const {
524+
llvm_unreachable("not yet implemented");
525+
}
526+
527+
cir::ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType ty) const {
528+
llvm_unreachable("not yet implemented");
529+
}
530+
531+
void AMDGPUABIInfo::computeInfo(CIRGenFunctionInfo &fnInfo) const {
532+
// Top level CIR has unlimited arguments and return types. Lowering for ABI
533+
// specific concerns should happen during a lowering phase. Assume everything
534+
// is direct for now.
535+
for (CIRGenFunctionInfo::arg_iterator it = fnInfo.arg_begin(),
536+
ie = fnInfo.arg_end();
537+
it != ie; ++it) {
538+
if (testIfIsVoidTy(it->type))
539+
it->info = cir::ABIArgInfo::getIgnore();
540+
else
541+
it->info = cir::ABIArgInfo::getDirect(CGT.convertType(it->type));
542+
}
543+
auto retTy = fnInfo.getReturnType();
544+
if (testIfIsVoidTy(retTy))
545+
fnInfo.getReturnInfo() = cir::ABIArgInfo::getIgnore();
546+
else
547+
fnInfo.getReturnInfo() = cir::ABIArgInfo::getDirect(CGT.convertType(retTy));
548+
}
549+
498550
ABIInfo::~ABIInfo() {}
499551

500552
bool ABIInfo::isPromotableIntegerTypeForABI(QualType Ty) const {
@@ -690,5 +742,9 @@ const TargetCIRGenInfo &CIRGenModule::getTargetCIRGenInfo() {
690742
case llvm::Triple::nvptx64: {
691743
return SetCIRGenInfo(new NVPTXTargetCIRGenInfo(genTypes));
692744
}
745+
746+
case llvm::Triple::amdgcn: {
747+
return SetCIRGenInfo(new AMDGPUTargetCIRGenInfo(genTypes));
748+
}
693749
}
694750
}

‎clang/test/CIR/CodeGen/HIP/simple.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "../Inputs/cuda.h"
2+
3+
// RUN: %clang_cc1 -triple=amdgcn-amd-amdhsa -x hip -fclangir \
4+
// RUN: -emit-cir %s -o %t.cir
5+
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
6+
7+
8+
// This should emit as a normal C++ function.
9+
__host__ void host_fn(int *a, int *b, int *c) {}
10+
11+
// CIR: cir.func @_Z7host_fnPiS_S_
12+
13+
// This shouldn't emit.
14+
__device__ void device_fn(int* a, double b, float c) {}
15+
16+
// CHECK-NOT: cir.func @_Z9device_fnPidf

0 commit comments

Comments
 (0)
Please sign in to comment.