-
Notifications
You must be signed in to change notification settings - Fork 796
[SYCL][NATIVECPU] Materialize floating point atomics in LLVM pass #15888
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b58c27d
[SYCL][NATIVECPU] Materialize floating point atomic builtins
PietroGhg 5167331
Add lit test
PietroGhg 5331f3f
Add comment
PietroGhg 20736d3
formatting
PietroGhg 0838022
Update test
PietroGhg da56c40
Merge branch 'sycl' into pietro/fp_atomics
PietroGhg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| //===------- FAtomicsNativeCPU.h - Materializes FP Atomics ----------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // A transformation pass that materializes floating points atomics by emitting | ||
| // corresponding atomicrmw instruction. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "llvm/IR/Module.h" | ||
| #include "llvm/IR/PassManager.h" | ||
|
|
||
| namespace llvm { | ||
|
|
||
| class ModulePass; | ||
|
|
||
| class FAtomicsNativeCPU | ||
| : public PassInfoMixin<FAtomicsNativeCPU> { | ||
| public: | ||
| PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); | ||
| }; | ||
|
|
||
| } // namespace llvm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| //===------- FAtomicsNativeCPU.cpp - Materializes FP Atomics --------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // A transformation pass that materializes floating points atomics by emitting | ||
| // corresponding atomicrmw instruction. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "llvm/SYCLLowerIR/FAtomicsNativeCPU.h" | ||
| #include "llvm/IR/IRBuilder.h" | ||
| #include "llvm/IR/Instructions.h" | ||
| #include "llvm/IR/LLVMContext.h" | ||
| #include "llvm/Support/Alignment.h" | ||
| #include "llvm/Support/AtomicOrdering.h" | ||
|
|
||
| using namespace llvm; | ||
|
|
||
| PreservedAnalyses FAtomicsNativeCPU::run(Module &M, | ||
| ModuleAnalysisManager &MAM) { | ||
| bool ModuleChanged = false; | ||
| auto &Ctx = M.getContext(); | ||
| for (auto &F : M) { | ||
| AtomicRMWInst::BinOp OpCode; | ||
| if (F.getName().starts_with("_Z21__spirv_AtomicFAddEXT")) { | ||
| OpCode = AtomicRMWInst::BinOp::FAdd; | ||
| } else if (F.getName().starts_with("_Z21__spirv_AtomicFMinEXT")) { | ||
| OpCode = AtomicRMWInst::BinOp::FMin; | ||
| } else if (F.getName().starts_with("_Z21__spirv_AtomicFMaxEXT")) { | ||
| OpCode = AtomicRMWInst::BinOp::FMax; | ||
| } else { | ||
| continue; | ||
| } | ||
|
|
||
| BasicBlock *BB = BasicBlock::Create(Ctx, "entry", &F); | ||
| IRBuilder<> Builder(BB); | ||
| // Currently we drop arguments 1 and 2 (scope and memory ordering), | ||
| // defaulting to Monotonic ordering and System scope. | ||
| auto A = | ||
| Builder.CreateAtomicRMW(OpCode, F.getArg(0), F.getArg(3), MaybeAlign(), | ||
| AtomicOrdering::Monotonic, SyncScope::System); | ||
| Builder.CreateRet(A); | ||
| } | ||
| return ModuleChanged ? PreservedAnalyses::none() : PreservedAnalyses::all(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // RUN: %clangxx -fsycl-device-only -fsycl-targets=native_cpu -S -emit-llvm -o %t_temp.ll %s | ||
| // RUN: %clangxx -mllvm -sycl-native-cpu-backend -S -emit-llvm -o - %t_temp.ll | FileCheck %s | ||
| #include <sycl/sycl.hpp> | ||
|
|
||
| constexpr sycl::memory_order order = sycl::memory_order::relaxed; | ||
| constexpr sycl::memory_scope scope = sycl::memory_scope::work_group; | ||
| constexpr sycl::access::address_space space = | ||
| sycl::access::address_space::global_space; | ||
|
|
||
| class Test; | ||
| using namespace sycl; | ||
| int main() { | ||
| queue q; | ||
| const size_t N = 32; | ||
| float sum = 0; | ||
| std::vector<float> output(N); | ||
| std::fill(output.begin(), output.end(), 0.f); | ||
| { | ||
| buffer<float> sum_buf(&sum, 1); | ||
| q.submit([&](handler &cgh) { | ||
| auto sum = sum_buf.template get_access<access::mode::read_write>(cgh); | ||
| cgh.parallel_for<Test>(range<1>(N), [=](item<1> it) { | ||
| int gid = it.get_id(0); | ||
| auto atm = atomic_ref<float, order, scope, space>(sum[0]); | ||
| atm.fetch_add(1.f, order); | ||
| //CHECK-DAG: float @_Z21__spirv_AtomicFAddEXT{{.*}}(ptr {{.*}} %[[ARG0:.*]], i32 {{.*}}, i32 {{.*}}, float {{.*}} %[[ARG3:.*]]) | ||
| //CHECK: %[[RES:.*]] = atomicrmw fadd ptr addrspace(1) %[[ARG0]], float %[[ARG3]] monotonic, align 4 | ||
| //CHECK: ret float %[[RES]] | ||
| atm.fetch_max(1.f, order); | ||
| //CHECK-DAG: float @_Z21__spirv_AtomicFMaxEXT{{.*}}(ptr {{.*}} %[[ARG0:.*]], i32 {{.*}}, i32 {{.*}}, float {{.*}} %[[ARG3:.*]]) | ||
| //CHECK: %[[RES:.*]] = atomicrmw fmax ptr addrspace(1) %[[ARG0]], float %[[ARG3]] monotonic, align 4 | ||
| //CHECK: ret float %[[RES]] | ||
| atm.fetch_min(1.f, order); | ||
| //CHECK-DAG: float @_Z21__spirv_AtomicFMinEXT{{.*}}(ptr {{.*}} %[[ARG0:.*]], i32 {{.*}}, i32 {{.*}}, float {{.*}} %[[ARG3:.*]]) | ||
| //CHECK: %[[RES:.*]] = atomicrmw fmin ptr addrspace(1) %[[ARG0]], float %[[ARG3]] monotonic, align 4 | ||
| //CHECK: ret float %[[RES]] | ||
| }); | ||
| }).wait_and_throw(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a FE test? I assume there is already a FE test checking the existence of this macro for other targets. You can just add a RUN there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added it, thank you