1- From 8a7885884d8b7074f716332768ed957d849b9a72 Mon Sep 17 00:00:00 2001
1+ From cc687f3c2399b83da0156ff24d09c24bab31e74e Mon Sep 17 00:00:00 2001
22From: haonanya <
[email protected] >
33Date: Wed, 28 Jul 2021 14:24:23 +0800
44Subject: [PATCH] Add support for cl_ext_float_atomics in SPIRVWriter
77---
88 include/LLVMSPIRVExtensions.inc | 1 +
99 lib/SPIRV/OCLToSPIRV.cpp | 27 +++++-
10- lib/SPIRV/OCLUtil.cpp | 19 ++--
10+ lib/SPIRV/OCLUtil.cpp | 15 ++--
1111 lib/SPIRV/SPIRVToOCL.h | 3 +
1212 lib/SPIRV/SPIRVToOCL12.cpp | 21 +++++
1313 lib/SPIRV/SPIRVToOCL20.cpp | 28 +++++-
1616 lib/SPIRV/libSPIRV/SPIRVOpCode.h | 8 +-
1717 lib/SPIRV/libSPIRV/SPIRVOpCodeEnum.h | 2 +
1818 lib/SPIRV/libSPIRV/spirv.hpp | 7 ++
19- test/AtomicBuiltinsFloat.ll | 79 ++++++++++++++++
19+ test/AtomicBuiltinsFloat.ll | 94 +++ ++++++++++++++++
2020 test/AtomicFAddEXT.ll | 72 +++++++++++++++
2121 test/AtomicFAddEXTForOCL.ll | 84 +++++++++++++++++
2222 test/AtomicFAddExt.ll | 119 -------------------------
2525 test/AtomicFMinEXT.ll | 73 +++++++++++++++
2626 test/AtomicFMinEXTForOCL.ll | 81 +++++++++++++++++
2727 test/negative/InvalidAtomicBuiltins.cl | 18 +---
28- 20 files changed, 675 insertions(+), 148 deletions(-)
28+ 20 files changed, 688 insertions(+), 146 deletions(-)
2929 create mode 100644 test/AtomicBuiltinsFloat.ll
3030 create mode 100644 test/AtomicFAddEXT.ll
3131 create mode 100644 test/AtomicFAddEXTForOCL.ll
@@ -100,30 +100,28 @@ index 7c65b9e8..7ea350ff 100644
100100 &Attrs);
101101 }
102102diff --git a/lib/SPIRV/OCLUtil.cpp b/lib/SPIRV/OCLUtil.cpp
103- index 2cc5d815..e4d7a7cf 100644
103+ index 2cc5d815..89ae7fe7 100644
104104--- a/lib/SPIRV/OCLUtil.cpp
105105+++ b/lib/SPIRV/OCLUtil.cpp
106106@@ -655,29 +655,32 @@ size_t getSPIRVAtomicBuiltinNumMemoryOrderArgs(Op OC) {
107107 return 1;
108108 }
109109
110- + // atomic_fetch_[add, sub, min, max] and atomic_fetch_[add, sub, min,
111- + // max]_explicit functions are defined on OpenCL headers, they are not
112- + // translated to function call
110+ + // atomic_fetch_[add, min, max] and atomic_fetch_[add, min, max]_explicit
111+ + // functions declared in clang headers should be translated to corresponding
112+ + // FP-typed Atomic Instructions
113113 bool isComputeAtomicOCLBuiltin(StringRef DemangledName) {
114114 if (!DemangledName.startswith(kOCLBuiltinName::AtomicPrefix) &&
115115 !DemangledName.startswith(kOCLBuiltinName::AtomPrefix))
116116 return false;
117117
118118 return llvm::StringSwitch<bool>(DemangledName)
119119- .EndsWith("add", true)
120- - .EndsWith("sub", true)
120+ .EndsWith("sub", true)
121121+ .EndsWith("atomic_add", true)
122- + .EndsWith("atomic_sub", true)
123122+ .EndsWith("atomic_min", true)
124123+ .EndsWith("atomic_max", true)
125124+ .EndsWith("atom_add", true)
126- + .EndsWith("atom_sub", true)
127125+ .EndsWith("atom_min", true)
128126+ .EndsWith("atom_max", true)
129127 .EndsWith("inc", true)
@@ -135,7 +133,7 @@ index 2cc5d815..e4d7a7cf 100644
135133 .EndsWith("or", true)
136134 .EndsWith("xor", true)
137135- .EndsWith("add_explicit", true)
138- - .EndsWith("sub_explicit", true)
136+ .EndsWith("sub_explicit", true)
139137 .EndsWith("or_explicit", true)
140138 .EndsWith("xor_explicit", true)
141139 .EndsWith("and_explicit", true)
@@ -385,11 +383,13 @@ index f0e311c6..2a86f32e 100644
385383 case OpVmeImageINTEL: *hasResult = true; *hasResultType = true; break;
386384diff --git a/test/AtomicBuiltinsFloat.ll b/test/AtomicBuiltinsFloat.ll
387385new file mode 100644
388- index 00000000..d9300558
386+ index 00000000..778c0cb0
389387--- /dev/null
390388+++ b/test/AtomicBuiltinsFloat.ll
391- @@ -0,0 +1,79 @@
389+ @@ -0,0 +1,94 @@
392390+ ; Check that translator generate atomic instructions for atomic builtins
391+ + ; FP-typed atomic_fetch_sub and atomic_fetch_sub_explicit should be translated
392+ + ; to FunctionCall
393393+ ; RUN: llvm-as %s -o %t.bc
394394+ ; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck %s
395395+ ; RUN: llvm-spirv %t.bc -o %t.spv
@@ -400,12 +400,13 @@ index 00000000..d9300558
400400+ ; CHECK-COUNT-3: AtomicStore
401401+ ; CHECK-COUNT-3: AtomicLoad
402402+ ; CHECK-COUNT-3: AtomicExchange
403+ + ; CHECK-COUNT-3: FunctionCall
403404+
404405+ target datalayout = "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
405406+ target triple = "spir-unknown-unknown"
406407+
407408+ ; Function Attrs: convergent norecurse nounwind
408- + define dso_local spir_kernel void @test_atomic_kernel(float addrspace(3)* %ff, float addrspace(3)* nocapture readnone %a ) local_unnamed_addr #0 !kernel_arg_addr_space !3 !kernel_arg_access_qual !4 !kernel_arg_type !5 !kernel_arg_base_type !6 !kernel_arg_type_qual !7 {
409+ + define dso_local spir_kernel void @test_atomic_kernel(float addrspace(3)* %ff) local_unnamed_addr #0 !kernel_arg_addr_space !3 !kernel_arg_access_qual !4 !kernel_arg_type !5 !kernel_arg_base_type !6 !kernel_arg_type_qual !7 {
409410+ entry:
410411+ %0 = addrspacecast float addrspace(3)* %ff to float addrspace(4)*
411412+ tail call spir_func void @_Z11atomic_initPU3AS4VU7_Atomicff(float addrspace(4)* %0, float 1.000000e+00) #2
@@ -418,6 +419,9 @@ index 00000000..d9300558
418419+ %call3 = tail call spir_func float @_Z15atomic_exchangePU3AS4VU7_Atomicff(float addrspace(4)* %0, float 1.000000e+00) #2
419420+ %call4 = tail call spir_func float @_Z24atomic_exchange_explicitPU3AS4VU7_Atomicff12memory_order(float addrspace(4)* %0, float 1.000000e+00, i32 0) #2
420421+ %call5 = tail call spir_func float @_Z24atomic_exchange_explicitPU3AS4VU7_Atomicff12memory_order12memory_scope(float addrspace(4)* %0, float 1.000000e+00, i32 0, i32 1) #2
422+ + %call6 = tail call spir_func float @_Z16atomic_fetch_subPU3AS3VU7_Atomicff(float addrspace(3)* %ff, float 1.000000e+00) #2
423+ + %call7 = tail call spir_func float @_Z25atomic_fetch_sub_explicitPU3AS3VU7_Atomicff12memory_order(float addrspace(3)* %ff, float 1.000000e+00, i32 0) #2
424+ + %call8 = tail call spir_func float @_Z25atomic_fetch_sub_explicitPU3AS3VU7_Atomicff12memory_order12memory_scope(float addrspace(3)* %ff, float 1.000000e+00, i32 0, i32 1) #2
421425+ ret void
422426+ }
423427+
@@ -451,6 +455,15 @@ index 00000000..d9300558
451455+ ; Function Attrs: convergent
452456+ declare spir_func float @_Z24atomic_exchange_explicitPU3AS4VU7_Atomicff12memory_order12memory_scope(float addrspace(4)*, float, i32, i32) local_unnamed_addr #1
453457+
458+ + ; Function Attrs: convergent
459+ + declare spir_func float @_Z16atomic_fetch_subPU3AS3VU7_Atomicff(float addrspace(3)*, float) local_unnamed_addr #1
460+ +
461+ + ; Function Attrs: convergent
462+ + declare spir_func float @_Z25atomic_fetch_sub_explicitPU3AS3VU7_Atomicff12memory_order(float addrspace(3)*, float, i32) local_unnamed_addr #1
463+ +
464+ + ; Function Attrs: convergent
465+ + declare spir_func float @_Z25atomic_fetch_sub_explicitPU3AS3VU7_Atomicff12memory_order12memory_scope(float addrspace(3)*, float, i32, i32) local_unnamed_addr #1
466+ +
454467+ attributes #0 = { convergent norecurse nounwind "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "uniform-work-group-size"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
455468+ attributes #1 = { convergent "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
456469+ attributes #2 = { convergent nounwind }
@@ -463,11 +476,11 @@ index 00000000..d9300558
463476+ !0 = !{i32 1, !"wchar_size", i32 4}
464477+ !1 = !{i32 2, i32 0}
465478+ !2 = !{!"clang version 12.0.1 (https://github.com/llvm/llvm-project.git 23fe7b104a0adaaaecd52108105f49297c420c9b)"}
466- + !3 = !{i32 3, i32 3 }
467- + !4 = !{!"none", !"none" }
468- + !5 = !{!"atomic_float*", !"float*" }
469- + !6 = !{!"_Atomic(float)*", !"float*" }
470- + !7 = !{!"volatile", !"" }
479+ + !3 = !{i32 3}
480+ + !4 = !{!"none"}
481+ + !5 = !{!"atomic_float*"}
482+ + !6 = !{!"_Atomic(float)*"}
483+ + !7 = !{!"volatile"}
471484diff --git a/test/AtomicFAddEXT.ll b/test/AtomicFAddEXT.ll
472485new file mode 100644
473486index 00000000..b012c904
0 commit comments