Skip to content

Commit b58c27d

Browse files
committed
[SYCL][NATIVECPU] Materialize floating point atomic builtins
1 parent 51f8a05 commit b58c27d

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,8 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
15131513
const llvm::Triple::SubArchType DeviceSubArch = DeviceTriple.getSubArch();
15141514
if (DeviceTriple.isNVPTX() || DeviceTriple.isAMDGPU() ||
15151515
(DeviceTriple.isSPIR() &&
1516-
DeviceSubArch != llvm::Triple::SPIRSubArch_fpga))
1516+
DeviceSubArch != llvm::Triple::SPIRSubArch_fpga) ||
1517+
LangOpts.SYCLIsNativeCPU)
15171518
Builder.defineMacro("SYCL_USE_NATIVE_FP_ATOMICS");
15181519
// Enable generation of USM address spaces for FPGA.
15191520
if (DeviceSubArch == llvm::Triple::SPIRSubArch_fpga) {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===------- FAtomicsNativeCPU.h - Materializes FP Atomics ----------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// A transformation pass that materializes floating points atomics by emitting
10+
// corresponding atomicrmw instruction.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#pragma once
15+
16+
#include "llvm/IR/Module.h"
17+
#include "llvm/IR/PassManager.h"
18+
19+
namespace llvm {
20+
21+
class ModulePass;
22+
23+
class FAtomicsNativeCPU
24+
: public PassInfoMixin<FAtomicsNativeCPU> {
25+
public:
26+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
27+
};
28+
29+
} // namespace llvm

llvm/lib/SYCLNativeCPUUtils/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ add_llvm_component_library(LLVMSYCLNativeCPUUtils
44
RenameKernelSYCLNativeCPU.cpp
55
ConvertToMuxBuiltinsSYCLNativeCPU.cpp
66
FixABIMuxBuiltinsSYCLNativeCPU.cpp
7+
FAtomicsNativeCPU.cpp
78

89
ADDITIONAL_HEADER_DIRS
910
${LLVM_MAIN_INCLUDE_DIR}/llvm/SYCLLowerIR
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===------- FAtomicsNativeCPU.cpp - Materializes FP Atomics --------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// A transformation pass that materializes floating points atomics by emitting
10+
// corresponding atomicrmw instruction.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "llvm/SYCLLowerIR/FAtomicsNativeCPU.h"
15+
#include "llvm/IR/IRBuilder.h"
16+
#include "llvm/IR/Instructions.h"
17+
#include "llvm/IR/LLVMContext.h"
18+
#include "llvm/Support/Alignment.h"
19+
#include "llvm/Support/AtomicOrdering.h"
20+
21+
using namespace llvm;
22+
23+
PreservedAnalyses FAtomicsNativeCPU::run(Module &M,
24+
ModuleAnalysisManager &MAM) {
25+
bool ModuleChanged = false;
26+
auto &Ctx = M.getContext();
27+
for (auto &F : M) {
28+
AtomicRMWInst::BinOp OpCode;
29+
if (F.getName().starts_with("_Z21__spirv_AtomicFAddEXT")) {
30+
OpCode = AtomicRMWInst::BinOp::FAdd;
31+
} else if (F.getName().starts_with("_Z21__spirv_AtomicFMinEXT")) {
32+
OpCode = AtomicRMWInst::BinOp::FMin;
33+
} else if (F.getName().starts_with("_Z21__spirv_AtomicFMaxEXT")) {
34+
OpCode = AtomicRMWInst::BinOp::FMax;
35+
} else {
36+
continue;
37+
}
38+
39+
BasicBlock *BB = BasicBlock::Create(Ctx, "entry", &F);
40+
IRBuilder<> Builder(BB);
41+
auto A =
42+
Builder.CreateAtomicRMW(OpCode, F.getArg(0), F.getArg(3), MaybeAlign(),
43+
AtomicOrdering::Monotonic, SyncScope::System);
44+
Builder.CreateRet(A);
45+
}
46+
return ModuleChanged ? PreservedAnalyses::none() : PreservedAnalyses::all();
47+
}

llvm/lib/SYCLNativeCPUUtils/PipelineSYCLNativeCPU.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// When NATIVECPU_USE_OCK is set, adds passes from the oneAPI Construction Kit.
1212
//
1313
//===----------------------------------------------------------------------===//
14+
#include "llvm/SYCLLowerIR/FAtomicsNativeCPU.h"
1415
#include "llvm/SYCLLowerIR/ConvertToMuxBuiltinsSYCLNativeCPU.h"
1516
#include "llvm/SYCLLowerIR/FixABIMuxBuiltinsSYCLNativeCPU.h"
1617
#include "llvm/SYCLLowerIR/PrepareSYCLNativeCPU.h"
@@ -70,6 +71,7 @@ void llvm::sycl::utils::addSYCLNativeCPUBackendPasses(
7071
OptimizationLevel OptLevel) {
7172
MPM.addPass(SpecConstantsPass(SpecConstantsPass::HandlingMode::emulation));
7273
MPM.addPass(ConvertToMuxBuiltinsSYCLNativeCPUPass());
74+
MPM.addPass(FAtomicsNativeCPU());
7375
#ifdef NATIVECPU_USE_OCK
7476
MPM.addPass(compiler::utils::PrepareBarriersPass());
7577
MPM.addPass(compiler::utils::TransferKernelMetadataPass());

0 commit comments

Comments
 (0)