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

[SYClomatic]PTX asm reduction #2506

Open
wants to merge 1 commit into
base: SYCLomatic
Choose a base branch
from
Open
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
136 changes: 136 additions & 0 deletions clang/lib/DPCT/RulesAsm/AsmMigration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2641,7 +2641,143 @@ class SYCLGen : public SYCLGenBase {
endstmt();
return SYCLGenSuccess();
}

bool handle_red(const InlineAsmInstruction *Inst) override {
// FIXME: inc, dec operation not supported now.
if (Inst->getNumInputOperands() > 1)
return SYCLGenError();
if (emitStmt(Inst->getOutputOperand()))
return SYCLGenError();
OS() << " = " << MapNames::getDpctNamespace() << "atomic_fetch_";
if (Inst->hasAttr(InstAttr::add))
OS() << "add";
else if (Inst->hasAttr(InstAttr::min))
OS() << "min";
else if (Inst->hasAttr(InstAttr::max))
OS() << "max";
else if (Inst->hasAttr(InstAttr::or))
OS() << "or";
else if (Inst->hasAttr(InstAttr::xor))
OS() << "xor";
else if (Inst->hasAttr(InstAttr::and))
OS() << "and";
else
return SYCLGenError();
OS()<< '<';
if (Inst->hasAttr(InstAttr::global)) {
if(Inst->hasAttr(InstAttr::relaxed)){
if(Inst->hasAttr(InstAttr::cta)){

OS() << MapNames::getClNamespace() << "access::address_space::global_space,"
<< MapNames::getClNamespace() << "memory_order::relaxed,"
<< MapNames::getClNamespace() << "memory_scope::work_group";
}

else if(Inst->hasAttr(InstAttr::gpu)){

OS() << MapNames::getClNamespace() << "access::address_space::global_space,"
<< MapNames::getClNamespace() << "memory_order::relaxed,"
<< MapNames::getClNamespace() << "memory_scope::device";
}

else if(Inst->hasAttr(InstAttr::sys)){

OS() << MapNames::getClNamespace() << "access::address_space::global_space,"
<< MapNames::getClNamespace() << "memory_order::relaxed,"
<< MapNames::getClNamespace() << "memory_scope::system";
}

}
else if(Inst->hasAttr(InstAttr::release)){
if(Inst->hasAttr(InstAttr::cta)){

OS() << MapNames::getClNamespace() << "access::address_space::global_space,"
<< MapNames::getClNamespace() << "memory_order::release,"
<< MapNames::getClNamespace() << "memory_scope::work_group";
}

else if(Inst->hasAttr(InstAttr::gpu)){

OS() << MapNames::getClNamespace() << "access::address_space::global_space,"
<< MapNames::getClNamespace() << "memory_order::release,"
<< MapNames::getClNamespace() << "memory_scope::device";
}

else if(Inst->hasAttr(InstAttr::sys)){

OS() << MapNames::getClNamespace() << "access::address_space::global_space,"
<< MapNames::getClNamespace() << "memory_order::release,"
<< MapNames::getClNamespace() << "memory_scope::system";
}

}
}
else if (Inst->hasAttr(InstAttr::shared)) {
if(Inst->hasAttr(InstAttr::relaxed)){
if(Inst->hasAttr(InstAttr::cta)){

OS() << MapNames::getClNamespace() << "access::address_space::local_space,"
<< MapNames::getClNamespace() << "memory_order::relaxed,"
<< MapNames::getClNamespace() << "memory_scope::work_group";
}

else if(Inst->hasAttr(InstAttr::gpu)){

OS() << MapNames::getClNamespace() << "access::address_space::local_space,"
<< MapNames::getClNamespace() << "memory_order::relaxed,"
<< MapNames::getClNamespace() << "memory_scope::device";
}

else if(Inst->hasAttr(InstAttr::sys)){

OS() << MapNames::getClNamespace() << "access::address_space::local_space,"
<< MapNames::getClNamespace() << "memory_order::relaxed,"
<< MapNames::getClNamespace() << "memory_scope::system";
}

}
else if(Inst->hasAttr(InstAttr::release)){
if(Inst->hasAttr(InstAttr::cta)){

OS() << MapNames::getClNamespace() << "access::address_space::local_space,"
<< MapNames::getClNamespace() << "memory_order::release,"
<< MapNames::getClNamespace() << "memory_scope::work_group";
}

else if(Inst->hasAttr(InstAttr::gpu)){

OS() << MapNames::getClNamespace() << "access::address_space::local_space,"
<< MapNames::getClNamespace() << "memory_order::release,"
<< MapNames::getClNamespace() << "memory_scope::device";
}

else if(Inst->hasAttr(InstAttr::sys)){

OS() << MapNames::getClNamespace() << "access::address_space::local_space,"
<< MapNames::getClNamespace() << "memory_order::release,"
<< MapNames::getClNamespace() << "memory_scope::system";
}

}

}
OS() << '>';
OS() << '(';
llvm::SaveAndRestore<const InlineAsmInstruction *> Save(CurrInst);
CurrInst = Inst;
for (const auto &[I, Op] : llvm::enumerate(Inst->input_operands())) {
if (emitStmt(Op))
return SYCLGenError();
if (I != Inst->getNumInputOperands() - 1)
OS() << ", ";
}

OS() << ')';
endstmt();
insertHeader(HeaderType::HT_DPCT_Atomic);
return SYCLGenSuccess();
}

bool handle_atom(const InlineAsmInstruction *Inst) override {
// FIXME: CAS operation was not supported now.
if (Inst->getNumInputOperands() > 2)
Expand Down
51 changes: 51 additions & 0 deletions clang/test/dpct/asm/red.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// UNSUPPORTED: cuda-8.0, cuda-9.0, cuda-9.1, cuda-9.2, cuda-10.0, cuda-10.1, cuda-10.2
// UNSUPPORTED: v8.0, v9.0, v9.1, v9.2, v10.0, v10.1, v10.2
// RUN: dpct --format-range=none -out-root %T/red %s --cuda-include-path="%cuda-path/include" -- -std=c++14 -x cuda --cuda-host-only
// RUN: FileCheck %s --match-full-lines --input-file %T/atom/red.dp.cpp
// RUN: %if build_lit %{icpx -c -fsycl %T/red/red.dp.cpp -o %T/red/red.dp.o %}

// clang-format off
#include <cuda_runtime.h>
#include <cstdint>

/*
.space = { .global, .shared{::cta, ::cluster} };
.type = { .b32, .b64, .u32, .u64, .s32, .s64, .f32, .f64 };
.scope = { .cta, .cluster, .gpu, .sys };
.sem = { .release, .relaxed };

Current only support the form likes "red.sem.scope.space.type" now.

*/

__global__ void red(int *a) {
int a = 0;

// CHECK: d = dpct::atomic_fetch_add<sycl::access::address_space::global_space, sycl::memory_order::relaxed, sycl::memory_scope::device>(a, 1);
asm volatile ("red.relaxed.gpu.global.add.s32 %0, [%1];" : "=r"(a) : "l"(a), "r"(1));

// CHECK: d = dpct::atomic_fetch_min<sycl::access::address_space::global_space, sycl::memory_order::relaxed, sycl::memory_scope::device>(a, 1);
asm volatile ("red.relaxed.gpu.global.min.s32 %0, [%1];" : "=r"(a) : "l"(a), "r"(1));

// CHECK: d = dpct::atomic_fetch_max<sycl::access::address_space::global_space, sycl::memory_order::relaxed, sycl::memory_scope::device>(a, 1);
asm volatile ("red.relaxed.gpu.global.max.s32 %0, [%1];" : "=r"(a) : "l"(a), "r"(1));

// CHECK: d = dpct::atomic_fetch_or<sycl::access::address_space::global_space, sycl::memory_order::relaxed, sycl::memory_scope::device>(a, 1);
asm volatile ("red.relaxed.gpu.global.or.s32 %0, [%1];" : "=r"(a) : "l"(a), "r"(1));

// CHECK: d = dpct::atomic_fetch_xoe<sycl::access::address_space::global_space, sycl::memory_order::relaxed, sycl::memory_scope::device>(a, 1);
asm volatile ("red.relaxed.gpu.global.xor.s32 %0, [%1];" : "=r"(a) : "l"(a), "r"(1));

// CHECK: d = dpct::atomic_fetch_and<sycl::access::address_space::global_space, sycl::memory_order::relaxed, sycl::memory_scope::device>(a, 1);
asm volatile ("red.relaxed.gpu.global.and.s32 %0, [%1];" : "=r"(a) : "l"(a), "r"(1));

// CHECK: d = dpct::atomic_fetch_add<sycl::access::address_space::local_space, sycl::memory_order::relaxed, sycl::memory_scope::system>(a, 1);
asm volatile ("red.relaxed.sys.shared.add.s32 %0, [%1];" : "=r"(a) : "l"(a), "r"(1));

// CHECK: d = dpct::atomic_fetch_or<sycl::access::address_space::global_space, sycl::memory_order::release, sycl::memory_scope::device>(a, 1);
asm volatile ("red.release.gpu.global.or.s32 %0, [%1];" : "=r"(a) : "l"(a), "r"(1));


}

// clang-format on
Loading