|
| 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 | +} |
0 commit comments