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

[dcompute] add support for OpenCL image I/O #3835

Merged
merged 2 commits into from
Oct 11, 2021
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
4 changes: 3 additions & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ task:
rm llvm.tar.xz
clone_submodules_early_script: |
cd $CIRRUS_WORKING_DIR
git submodule update --init --depth $CIRRUS_CLONE_DEPTH
# remove --depth from following command because of
# https://github.com/ldc-developers/ldc/pull/3835#issuecomment-928517319
git submodule update --init # --depth $CIRRUS_CLONE_DEPTH
remove_some_tests_script: |
cd $CIRRUS_WORKING_DIR/tests/d2/dmd-testsuite
# The -lowmem tests don't work with an ltsmaster host compiler
Expand Down
1 change: 1 addition & 0 deletions dmd/id.d
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ immutable Msgtable[] msgtable =
{ "dcPointer", "Pointer" },
{ "dcReflect", "__dcompute_reflect" },
{ "RTInfoImpl" },
{ "opencl" },

// IN_LLVM
{ "io" },
Expand Down
1 change: 1 addition & 0 deletions dmd/id.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ struct Id
static Identifier *LDC_extern_weak;
static Identifier *LDC_profile_instr;
static Identifier *dcReflect;
static Identifier *opencl;
static Identifier *criticalenter;
static Identifier *criticalexit;
static Identifier *attributes;
Expand Down
11 changes: 9 additions & 2 deletions gen/dcompute/druntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "dmd/module.h"
#include "dmd/template.h"

bool isFromLDC_DCompute(Dsymbol *sym) {
bool isFromLDC_Mod(Dsymbol *sym, Identifier* id) {
auto mod = sym->getModule();
if (!mod)
return false;
Expand All @@ -31,7 +31,14 @@ bool isFromLDC_DCompute(Dsymbol *sym) {
if (moduleDecl->packages.ptr[0] != Id::ldc)
return false;

return moduleDecl->id == Id::dcompute;
return moduleDecl->id == id;
}

bool isFromLDC_DCompute(Dsymbol *sym) {
return isFromLDC_Mod(sym,Id::dcompute);
}
bool isFromLDC_OpenCL(Dsymbol *sym) {
return isFromLDC_Mod(sym,Id::opencl);
}

llvm::Optional<DcomputePointer> toDcomputePointer(StructDeclaration *sd) {
Expand Down
1 change: 1 addition & 0 deletions gen/dcompute/druntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Dsymbol;
class Type;

bool isFromLDC_DCompute(Dsymbol *sym);
bool isFromLDC_OpenCL(Dsymbol *sym);

struct DcomputePointer {
int addrspace;
Expand Down
86 changes: 52 additions & 34 deletions gen/dcompute/targetOCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "dmd/id.h"
#include "dmd/identifier.h"
#include "dmd/template.h"
#include "dmd/module.h"
#include "gen/abi-spirv.h"
#include "gen/dcompute/target.h"
#include "gen/dcompute/druntime.h"
Expand All @@ -38,6 +39,7 @@

namespace {
class TargetOCL : public DComputeTarget {
bool usedImage;
public:
TargetOCL(llvm::LLVMContext &c, int oclversion)
: DComputeTarget(c, oclversion, OpenCL, "ocl", "spv", createSPIRVABI(),
Expand All @@ -63,25 +65,35 @@ class TargetOCL : public DComputeTarget {
// opencl.used.optional.core.features
// opencl.compiler.options
// opencl.enable.FP_CONTRACT (-ffast-math)
llvm::Metadata *SPIRVerElts[] = {
{
llvm::Metadata *SPIRVerElts[] = {
llvm::ConstantAsMetadata::get(
llvm::ConstantInt::get(llvm::Type::getInt32Ty(ctx), 1)),
llvm::ConstantInt::get(llvm::Type::getInt32Ty(ctx), 1)),
llvm::ConstantAsMetadata::get(
llvm::ConstantInt::get(llvm::Type::getInt32Ty(ctx), 2))};
llvm::NamedMDNode *SPIRVerMD =
llvm::ConstantInt::get(llvm::Type::getInt32Ty(ctx), 2))};
llvm::NamedMDNode *SPIRVerMD =
_ir->module.getOrInsertNamedMetadata("opencl.spir.version");
SPIRVerMD->addOperand(llvm::MDNode::get(ctx, SPIRVerElts));

SPIRVerMD->addOperand(llvm::MDNode::get(ctx, SPIRVerElts));
}
// Add OpenCL version
llvm::Metadata *OCLVerElts[] = {
{
llvm::Metadata *OCLVerElts[] = {
llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
llvm::Type::getInt32Ty(ctx), tversion / 100)),
llvm::Type::getInt32Ty(ctx), tversion / 100)),
llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
llvm::Type::getInt32Ty(ctx), (tversion % 100) / 10))};
llvm::NamedMDNode *OCLVerMD =
llvm::Type::getInt32Ty(ctx), (tversion % 100) / 10))};
llvm::NamedMDNode *OCLVerMD =
_ir->module.getOrInsertNamedMetadata("opencl.ocl.version");

OCLVerMD->addOperand(llvm::MDNode::get(ctx, OCLVerElts));
OCLVerMD->addOperand(llvm::MDNode::get(ctx, OCLVerElts));
}
if (usedImage) {
llvm::NamedMDNode *OCLUsedCoreFeatures =
_ir->module.getOrInsertNamedMetadata("opencl.used.optional.core.features");
llvm::Metadata *OCLUsedCoreFeaturesElts[] = {
llvm::MDString::get(ctx,"cl_images")
};
OCLUsedCoreFeatures->addOperand(llvm::MDNode::get(ctx, OCLUsedCoreFeaturesElts));
}
}
enum KernArgMD {
KernArgMD_addr_space,
Expand All @@ -99,40 +111,25 @@ class TargetOCL : public DComputeTarget {

unsigned i = 0;
// TODO: Handle Function attibutes
llvm::SmallVector<llvm::Metadata *, 8> kernelMDArgs;
kernelMDArgs.push_back(llvm::ConstantAsMetadata::get(llf));
// MDNode for the kernel argument address space qualifiers.

std::array<llvm::SmallVector<llvm::Metadata *, 8>,count_KernArgMD> paramArgs;
std::array<const char*,count_KernArgMD> args = {{
std::array<llvm::StringRef,count_KernArgMD> args = {{
"kernel_arg_addr_space",
"kernel_arg_access_qual",
"kernel_arg_type",
"kernel_arg_type_qual",
"kernel_arg_base_type",
"kernel_arg_name"
}};

for (auto md : args) {
paramArgs[i].push_back(llvm::MDString::get(ctx, md));
i++;
}

VarDeclarations *vs = fd->parameters;
for (i = 0; i < vs->length; i++) {
VarDeclaration *v = (*vs)[i];
decodeTypes(paramArgs, v);
}

for (auto &md : paramArgs)
kernelMDArgs.push_back(llvm::MDNode::get(ctx, md));
///-------------------------------
/// TODO: Handle Function attibutes
///-------------------------------
llvm::MDNode *kernelMDNode = llvm::MDNode::get(ctx, kernelMDArgs);
llvm::NamedMDNode *OpenCLKernelMetadata =
_ir->module.getOrInsertNamedMetadata("opencl.kernels");
OpenCLKernelMetadata->addOperand(kernelMDNode);
for (i = 0; i < count_KernArgMD; i++) {
llf->setMetadata(args[i],llvm::MDNode::get(ctx,paramArgs[i]));
}
}

std::string mod2str(MOD mod) {
Expand Down Expand Up @@ -162,7 +159,7 @@ class TargetOCL : public DComputeTarget {
return ss.str();
}

void decodeTypes(std::array<llvm::SmallVector<llvm::Metadata *, 8>,count_KernArgMD> attrs,
void decodeTypes(std::array<llvm::SmallVector<llvm::Metadata *, 8>,count_KernArgMD>& attrs,
VarDeclaration *v)
{
llvm::Optional<DcomputePointer> ptr;
Expand All @@ -174,8 +171,29 @@ class TargetOCL : public DComputeTarget {
if (v->type->ty == TY::Tstruct &&
(ptr = toDcomputePointer(static_cast<TypeStruct *>(v->type)->sym))) {
addrspace = ptr->addrspace;
tyName = basicTypeToString(ptr->type) + "*";
baseTyName = tyName;
tyName = basicTypeToString(ptr->type);

auto ts = ptr->type->isTypeStruct();
if (ts && isFromLDC_OpenCL(ts->sym)) {
// TODO: Pipes
auto name = std::string(ts->toChars());
if (!usedImage && name.rfind("ldc.opencl.image",0) == 0)
usedImage = true;
// parse access qualifiers from ldc.opencl.*_XX_t types like ldc.opencl.image1d_ro_t
// 4 == length of "XX_t"
name = name.substr(name.length()-4, 2);
if (name == "ro")
accessQual = "read_only";
else if (name == "wo")
accessQual = "write_only";
else if (name == "rw")
accessQual = "read_write";
// ldc.opencl.{sampler_t, reserve_id_t} do not get an access qualifier
} else {
// things like e.g. GlobalPointer!opencl.image1d_[ro,wo,rw]_t/sampler_t must not have the additional *
// but all others e.g. GlobalPointer!int must
tyName += "*";
}
// there is no volatile or restrict (yet) in D
typeQuals = mod2str(ptr->type->mod);
// TODO: Images and Pipes They are global pointers to opaques
Expand Down
6 changes: 6 additions & 0 deletions ir/irtypestruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ IrTypeStruct *IrTypeStruct::get(StructDeclaration *sd) {

// if it's a forward declaration, all bets are off, stick with the opaque
if (sd->sizeok != Sizeok::done) {
// but rewrite the name for special OpenCL types
if (isFromLDC_OpenCL(sd)) {
const int prefixlen = 4; // == sizeof("ldc.")
LLStructType *st = static_cast<LLStructType *>(t->type);
st->setName(st->getName().substr(prefixlen));
}
return t;
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/druntime
43 changes: 43 additions & 0 deletions tests/codegen/dcompute_cl_images.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// REQUIRES: target_SPIRV
// RUN: %ldc -c -mdcompute-targets=ocl-220 -m64 -I%S/inputs -mdcompute-file-prefix=%t -output-ll -output-o %s && FileCheck %s < %t_ocl220_64.ll
@compute(CompileFor.deviceOnly) module dcompute_cl_images;
import ldc.dcompute;
import ldc.opencl;

// CHECK-DAG: %opencl.image1d_ro_t = type opaque
// CHECK-DAG: %opencl.image1d_wo_t = type opaque
// CHECK-DAG: %"ldc.dcompute.Pointer!(AddrSpace.Global, image1d_ro_t).Pointer" = type { %opencl.image1d_ro_t addrspace(1)* }
// CHECK-DAG: %"ldc.dcompute.Pointer!(AddrSpace.Global, image1d_wo_t).Pointer" = type { %opencl.image1d_wo_t addrspace(1)* }
// CHECK-DAG: %"ldc.dcompute.Pointer!(AddrSpace.Shared, sampler_t).Pointer" = type { %opencl.sampler_t addrspace(2)* }
// CHECK-DAG: %opencl.sampler_t = type opaque

pragma(mangle,"__translate_sampler_initializer")
Sampler makeSampler(int);

pragma(mangle,"_Z11read_imagef11ocl_image1d_ro11ocl_sampleri")
__vector(float[4]) read(GlobalPointer!image1d_ro_t, Sampler, int);

pragma(mangle,"_Z12write_imagef11ocl_image1d_woiDv4_f")
void write(GlobalPointer!image1d_wo_t,int,__vector(float[4]));

@kernel void img(GlobalPointer!image1d_ro_t src, GlobalPointer!image1d_wo_t dst)
{
// CHECK: %{{[0-9+]}} = call spir_func %opencl.sampler_t addrspace(2)* @__translate_sampler_initializer(i32 0) {{.*}}
// CHECK: %{{[0-9+]}} = call spir_func <4 x float> @_Z11read_imagef11ocl_image1d_ro11ocl_sampleri(%opencl.image1d_ro_t addrspace(1)* %.DComputePointerRewrite_arg, %opencl.sampler_t addrspace(2)* %.DComputePointerRewrite_arg1, i32 0) {{.*}}
auto x = src.read(makeSampler(0), 0);
// CHECK: call spir_func void @_Z12write_imagef11ocl_image1d_woiDv4_f(%opencl.image1d_wo_t addrspace(1)* %.DComputePointerRewrite_arg2, i32 0, <4 x float> %{{[0-9+]}})
dst.write(0,x);
}

@kernel void img2(GlobalPointer!image1d_ro_t src, Sampler samp)
{
// CHECK: %{{[0-9+]}} = call spir_func <4 x float> @_Z11read_imagef11ocl_image1d_ro11ocl_sampleri(%opencl.image1d_ro_t addrspace(1)* %.DComputePointerRewrite_arg, %opencl.sampler_t addrspace(2)* %.DComputePointerRewrite_arg1, i32 0) {{.*}}
auto x = src.read(samp, 0);
}

// metadata
// CHECK: !{{[0-9+]}} = !{!"read_only", !"write_only"}
// CHECK: !{{[0-9+]}} = !{!"image1d_ro_t", !"image1d_wo_t"}

// CHECK: !{{[0-9+]}} = !{!"read_only", !"none"}
// CHECK: !{{[0-9+]}} = !{!"image1d_ro_t", !"sampler_t"}