Skip to content

Commit b1a652a

Browse files
authored
[SYCLomatic][PTX] Support migration of PTX instruction add.f16x2 and sub.f16x2 (#2727)
Signed-off-by: chenwei.sun <chenwei.sun@intel.com>
1 parent d079d1c commit b1a652a

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

clang/lib/DPCT/RulesAsm/AsmMigration.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,10 +1043,12 @@ class SYCLGen : public SYCLGenBase {
10431043
BI->getKind() != InlineAsmBuiltinType::u32 &&
10441044
BI->getKind() != InlineAsmBuiltinType::s64 &&
10451045
BI->getKind() != InlineAsmBuiltinType::u64 &&
1046+
BI->getKind() != InlineAsmBuiltinType::f16x2 &&
10461047
BI->getKind() != InlineAsmBuiltinType::s16x2 &&
10471048
BI->getKind() != InlineAsmBuiltinType::u16x2)
10481049
return false;
10491050
isVec = BI->getKind() == InlineAsmBuiltinType::s16x2 ||
1051+
BI->getKind() == InlineAsmBuiltinType::f16x2 ||
10501052
BI->getKind() == InlineAsmBuiltinType::u16x2;
10511053
} else {
10521054
return false;
@@ -1088,12 +1090,21 @@ class SYCLGen : public SYCLGenBase {
10881090
OS() << MapNames::getClNamespace()
10891091
<< llvm::formatv("sub_sat({0}, {1})", Op[0], Op[1]);
10901092
} else {
1091-
if (Inst->is(asmtok::op_add))
1092-
OS() << llvm::formatv("{0} + {1}", Op[0], Op[1]);
1093-
else
1094-
OS() << llvm::formatv("{0} - {1}", Op[0], Op[1]);
1093+
if (const auto *BI = dyn_cast<InlineAsmBuiltinType>(Inst->getType(0))) {
1094+
std::string operatorStr = Inst->is(asmtok::op_add) ? "+" : "-";
1095+
1096+
if (BI->getKind() == InlineAsmBuiltinType::f16x2) {
1097+
std::string FormatTemp =
1098+
"(((sycl::vec<int, 1>({0})).as<sycl::vec<sycl::half, 2>>() {1} "
1099+
"(sycl::vec<int, 1>({2})).as<sycl::vec<sycl::half, "
1100+
"2>>()).as<sycl::vec<int, 1>>()).x();";
1101+
OS() << llvm::formatv(FormatTemp.c_str(), Op[0], operatorStr, Op[1]);
1102+
1103+
} else {
1104+
OS() << llvm::formatv("{0} {1} {2}", Op[0], operatorStr, Op[1]);
1105+
}
1106+
}
10951107
}
1096-
10971108
endstmt();
10981109
return SYCLGenSuccess();
10991110
}

clang/test/dpct/asm/add.cu

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,15 @@ __global__ void add() {
5959
asm("add.u16x2 %0, {1, 1}, %1;" : "=r"(u16x2) : "r"(ua));
6060
}
6161

62+
// CHECK: inline uint32_t add(uint32_t a, uint32_t b) {
63+
// CHECK-NEXT: uint32_t c;
64+
// CHECK-NEXT: c = (((sycl::vec<int, 1>(a)).as<sycl::vec<sycl::half, 2>>() + (sycl::vec<int, 1>(b)).as<sycl::vec<sycl::half, 2>>()).as<sycl::vec<int, 1>>()).x();;
65+
// CHECK-NEXT: return c;
66+
// CHECK-NEXT: }
67+
inline __device__ uint32_t add(uint32_t a, uint32_t b) {
68+
uint32_t c;
69+
asm volatile("add.f16x2 %0, %1, %2;\n" : "=r"(c) : "r"(a), "r"(b));
70+
return c;
71+
}
72+
6273
// clang-format off

clang/test/dpct/asm/sub.cu

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,15 @@ __global__ void sub() {
5959
asm("sub.u16x2 %0, {1, 1}, %1;" : "=r"(u16x2) : "r"(ua));
6060
}
6161

62+
// CHECK: inline uint32_t sub(uint32_t a, uint32_t b) {
63+
// CHECK-NEXT: uint32_t c;
64+
// CHECK-NEXT: c = (((sycl::vec<int, 1>(a)).as<sycl::vec<sycl::half, 2>>() - (sycl::vec<int, 1>(b)).as<sycl::vec<sycl::half, 2>>()).as<sycl::vec<int, 1>>()).x();;
65+
// CHECK-NEXT: return c;
66+
// CHECK-NEXT:}
67+
inline __device__ uint32_t sub(uint32_t a, uint32_t b) {
68+
uint32_t c;
69+
asm volatile("sub.f16x2 %0, %1, %2;\n" : "=r"(c) : "r"(a), "r"(b));
70+
return c;
71+
}
72+
6273
// clang-format on

0 commit comments

Comments
 (0)