Skip to content

Commit d11b841

Browse files
committed
dcompute: Prepare pointer ABI rewrite for Vulkan support
1 parent 76bf975 commit d11b841

3 files changed

Lines changed: 44 additions & 4 deletions

File tree

gen/dcompute/abi-rewrites.h

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
#include "gen/abi/generic.h"
1717

18+
#include "ir/irfuncty.h"
19+
20+
#include "llvm/IR/IntrinsicsSPIRV.h"
21+
1822
struct DComputePointerRewrite : ABIRewrite {
1923
LLValue *put(DValue *v, bool isLValueExp, bool) override {
2024
LLValue *address = DtoLVal(v);
@@ -23,12 +27,47 @@ struct DComputePointerRewrite : ABIRewrite {
2327

2428
LLValue *getLVal(Type *dty, LLValue *v) override {
2529
LLValue *mem = DtoAlloca(dty, ".DComputePointerRewrite_param_storage");
26-
DtoStore(v, mem);
30+
LLType *realStructTy = DtoMemType(dty);
31+
LLValue *field = DtoGEP(realStructTy, mem, 0u, 0u);
32+
33+
if (gIR && gIR->dcomputetarget &&
34+
gIR->dcomputetarget->target == DComputeTarget::ID::Vulkan) {
35+
auto ptrOpt = toDcomputePointer(static_cast<TypeStruct *>(dty)->sym);
36+
assert(ptrOpt && "DComputePointerRewrite expects ldc.dcompute.Pointer");
37+
const auto &ptr = *ptrOpt;
38+
llvm::Type *elemTy = DtoType(ptr.type);
39+
int realAS = gIR->dcomputetarget->mapping[ptr.addrspace];
40+
41+
llvm::Value *ofType = llvm::PoisonValue::get(elemTy);
42+
llvm::Metadata *md = llvm::ValueAsMetadata::get(ofType);
43+
llvm::Value *mdVal = llvm::MetadataAsValue::get(gIR->context(), md);
44+
45+
#if LLVM_VERSION_MAJOR >= 20
46+
llvm::Function *assignFn = llvm::Intrinsic::getOrInsertDeclaration(
47+
&gIR->module, llvm::Intrinsic::spv_assign_ptr_type, {v->getType()});
48+
#else
49+
llvm::Function *assignFn = llvm::Intrinsic::getDeclaration(
50+
&gIR->module, llvm::Intrinsic::spv_assign_ptr_type, {v->getType()});
51+
#endif
52+
gIR->ir->CreateCall(assignFn->getFunctionType(), assignFn,
53+
{v, mdVal, llvm::ConstantInt::get(llvm::Type::getInt32Ty(gIR->context()), realAS)});
54+
}
55+
56+
DtoStore(v, field);
2757
return mem;
2858
}
2959

3060
LLType *type(Type *t) override {
31-
auto ptr = toDcomputePointer(static_cast<TypeStruct *>(t)->sym);
32-
return ptr->toLLVMType(true);
61+
auto ptrOpt = toDcomputePointer(static_cast<TypeStruct *>(t)->sym);
62+
assert(ptrOpt && "DComputePointerRewrite expects ldc.dcompute.Pointer");
63+
return ptrOpt->toLLVMType(true);
64+
}
65+
66+
void applyTo(IrFuncTyArg &arg, LLType *finalLType = nullptr) override {
67+
arg.rewrite = this;
68+
arg.ltype = finalLType ? finalLType : this->type(arg.type);
69+
arg.byref = false;
70+
arg.attrs.removeAttribute(llvm::Attribute::Dereferenceable);
71+
arg.attrs.removeAttribute(llvm::Attribute::NonNull);
3372
}
3473
};

gen/dcompute/target.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class DComputeTarget {
2828
public:
2929
llvm::LLVMContext &ctx;
3030
int tversion; // OpenCL or CUDA CC version:major*100 + minor*10
31-
enum class ID { Host = 0, OpenCL = 1, CUDA = 2 };
31+
enum class ID { Host = 0, OpenCL = 1, CUDA = 2, Vulkan = 3 };
3232
ID target; // ID for codegen time conditional compilation.
3333
const char *short_name;
3434
const char *binSuffix;

runtime/druntime/src/ldc/dcompute.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ enum ReflectTarget : uint
1414
Host = 0,
1515
OpenCL = 1,
1616
CUDA = 2,
17+
Vulkan = 3,
1718
}
1819
/**
1920
* The pseudo conditional compilation function.

0 commit comments

Comments
 (0)