Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Unified Diff: lib/Transforms/NaCl/RewriteAtomics.cpp

Issue 17777004: Concurrency support for PNaCl ABI (Closed) Base URL: http://git.chromium.org/native_client/pnacl-llvm.git@master
Patch Set: Update PNaClLangRef to reflect the implementation work I will now go forward with. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/Transforms/NaCl/RewriteAtomics.cpp
diff --git a/lib/Transforms/NaCl/RewriteAtomics.cpp b/lib/Transforms/NaCl/RewriteAtomics.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..6936bf96ef01be9aa2833d9b6d502df51e99ca7c
--- /dev/null
+++ b/lib/Transforms/NaCl/RewriteAtomics.cpp
@@ -0,0 +1,252 @@
+//===- RewriteAtomics.cpp - Stabilize instructions used for concurrency ---===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This pass encodes atomics, volatiles and fences using NaCl intrinsics
+// instead of LLVM's regular IR instructions.
+//
+// All of the above are transformed into one of the
+// @llvm.nacl.atomic.<size> intrinsics.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/Twine.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/Intrinsics.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/NaClIntrinsics.h"
+#include "llvm/InstVisitor.h"
+#include "llvm/Pass.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/Transforms/NaCl.h"
+
+using namespace llvm;
+
+namespace {
+class RewriteAtomics : public ModulePass {
+public:
+ static char ID; // Pass identification, replacement for typeid
+ RewriteAtomics() : ModulePass(ID) {
+ // This is a module pass because it may have to introduce
+ // intrinsic declarations into the module and modify a global function.
+ initializeRewriteAtomicsPass(*PassRegistry::getPassRegistry());
+ }
+
+ virtual bool runOnModule(Module &M);
+};
+
+template <class T> Twine ToTwine(const T &V) {
+ std::string S;
+ raw_string_ostream OS(S);
+ OS << const_cast<T &>(V);
+ return OS.str();
+}
+
+class AtomicVisitor : public InstVisitor<AtomicVisitor> {
+ Module &M;
+ LLVMContext &C;
+ bool ModifiedModule;
+ struct {
+ Function *F;
+ unsigned BitSize;
+ } AtomicFunctions[NaCl::NumAtomicIntrinsics];
+
+ AtomicVisitor();
+ AtomicVisitor(const AtomicVisitor &);
+ AtomicVisitor &operator=(const AtomicVisitor &);
+
+ NaCl::MemoryOrder freezeMemoryOrdering(llvm::AtomicOrdering AO) const;
+ void checkSizeMatchesType(const Instruction &I, unsigned S,
+ const Type *T) const;
+ void checkAlignment(const Instruction &I, unsigned Alignment,
+ unsigned Size) const;
+ Function *atomicIntrinsic(const Instruction &I, unsigned AtomicBitSize);
+
+ // Replace atomic instruction I with a @llvm.nacl.atomic.<size> intrinsic.
+ //
+ // Type and Size are the base atomic integer type and its size in
+ // bits. The subsequent parameters are the ones passed to the
+ // intrinsic, and are detailed in docs/PNaClLangRef.rst.
+ void replaceWithAtomicIntrinsic(Instruction &I, const Type *T, unsigned Size,
+ NaCl::AtomicOperation O, Value *Loc,
+ Value *Val, Value *Old, AtomicOrdering AO);
+
+ // Most atomics instructions deal with at least one pointer, this
+ // struct automates some of this and has generic sanity checks.
+ template <class Instruction> struct PointerHelper {
+ Value *P;
+ Type *PET;
+ unsigned Size;
+ Value *Zero;
+ PointerHelper(const AtomicVisitor &AV, Instruction &I)
+ : P(I.getPointerOperand()) {
+ if (I.getPointerAddressSpace() != 0)
+ report_fatal_error("unhandled pointer address space " +
+ Twine(I.getPointerAddressSpace()) + " for atomic: " +
+ ToTwine(I));
+ assert(P->getType()->isPointerTy() && "expected a pointer");
+ PET = P->getType()->getPointerElementType();
+ Size = PET->getIntegerBitWidth();
+ AV.checkSizeMatchesType(I, Size, PET);
+ Zero = ConstantInt::get(PET, 0);
+ }
+ };
+
+public:
+ AtomicVisitor(Module &M) : M(M), C(M.getContext()), ModifiedModule(false) {
+ for (size_t i = 0; i != NaCl::NumAtomicIntrinsics; ++i) {
+ AtomicFunctions[i].F =
+ Intrinsic::getDeclaration(&M, NaCl::AtomicIntrinsics[i].ID);
+ AtomicFunctions[i].BitSize = NaCl::AtomicIntrinsics[i].BitSize;
+ }
+ }
+ ~AtomicVisitor() {}
+ bool modifiedModule() const { return ModifiedModule; }
+
+ void visitLoadInst(LoadInst &I);
+ void visitStoreInst(StoreInst &I);
+ void visitAtomicCmpXchgInst(AtomicCmpXchgInst &I);
+ void visitAtomicRMWInst(AtomicRMWInst &I);
+ void visitFenceInst(FenceInst &I);
+};
+}
+
+char RewriteAtomics::ID = 0;
+INITIALIZE_PASS(RewriteAtomics, "nacl-rewrite-atomics",
+ "rewrite atomics, volatiles and fences into stable "
+ "@llvm.nacl.atomics.<size> intrinsics",
+ false, false)
+
+bool RewriteAtomics::runOnModule(Module &M) {
+ AtomicVisitor AV(M);
+ AV.visit(M);
+ return AV.modifiedModule();
+}
+
+NaCl::MemoryOrder
+AtomicVisitor::freezeMemoryOrdering(llvm::AtomicOrdering AO) const {
+ // TODO For now only sequential consistency is allowed.
+ return NaCl::MemoryOrderSequentiallyConsistent;
+}
+
+void AtomicVisitor::checkSizeMatchesType(const Instruction &I, unsigned S,
+ const Type *T) const {
+ Type *IntType = Type::getIntNTy(C, S);
+ if (IntType && T == IntType)
+ return;
+ report_fatal_error("unsupported atomic type " + ToTwine(*T) + " of size " +
+ Twine(S) + " in: " + ToTwine(I));
+}
+
+void AtomicVisitor::checkAlignment(const Instruction &I, unsigned Alignment,
+ unsigned Size) const {
+ if (Alignment < Size)
+ report_fatal_error("atomic load/store must be at least naturally aligned, "
+ "got " + Twine(Alignment) + ", expected at least " +
+ Twine(Size) + ", in: " + ToTwine(I));
+}
+
+Function *AtomicVisitor::atomicIntrinsic(const Instruction &I,
+ unsigned AtomicBitSize) {
+ for (size_t Intr = 0; Intr != NaCl::NumAtomicIntrinsics; ++Intr)
+ if (AtomicFunctions[Intr].BitSize == AtomicBitSize)
+ return AtomicFunctions[Intr].F;
+ report_fatal_error("unsupported atomic bit size " + Twine(AtomicBitSize) +
+ " in : " + ToTwine(I));
+}
+
+void AtomicVisitor::replaceWithAtomicIntrinsic(Instruction &I, const Type *T,
+ unsigned Size,
+ NaCl::AtomicOperation O,
+ Value *Loc, Value *Val,
+ Value *Old, AtomicOrdering AO) {
+ Value *Args[] = { ConstantInt::get(Type::getInt32Ty(C), O), Loc, Val, Old,
+ ConstantInt::get(Type::getInt32Ty(C),
+ freezeMemoryOrdering(AO)) };
+ CallInst *Call = CallInst::Create(atomicIntrinsic(I, Size), Args, "", &I);
+ Call->setDebugLoc(I.getDebugLoc());
+ if (!I.getType()->isVoidTy())
+ I.replaceAllUsesWith(Call);
+ I.eraseFromParent();
+
+ ModifiedModule = true;
+}
+
+// %res = load {atomic|volatile} T* %ptr ordering, align sizeof(T)
+// becomes:
+// %res = call T @llvm.nacl.atomic.<sizeof(T)>(Load, %ptr, 0, 0, ordering)
+void AtomicVisitor::visitLoadInst(LoadInst &I) {
+ if (I.isSimple())
+ return;
+ PointerHelper<LoadInst> PH(*this, I);
+ checkAlignment(I, I.getAlignment() * 8, PH.Size);
+ replaceWithAtomicIntrinsic(I, PH.PET, PH.Size, NaCl::AtomicLoad, PH.P,
+ PH.Zero, PH.Zero, I.getOrdering());
+}
+
+// store {atomic|volatile} T %val, T* %ptr ordering, align sizeof(T)
+// becomes:
+// call T @llvm.nacl.atomic.<sizeof(T)>(Store, %ptr, %val, 0, ordering)
+void AtomicVisitor::visitStoreInst(StoreInst &I) {
+ if (I.isSimple())
+ return;
+ PointerHelper<StoreInst> PH(*this, I);
+ checkAlignment(I, I.getAlignment() * 8, PH.Size);
+ checkSizeMatchesType(I, PH.Size, I.getValueOperand()->getType());
+ replaceWithAtomicIntrinsic(I, PH.PET, PH.Size, NaCl::AtomicStore, PH.P,
+ I.getValueOperand(), PH.Zero, I.getOrdering());
+}
+
+// %res = cmpxchg T* %ptr, T %old, T %new ordering
+// becomes:
+// %res = call T @llvm.nacl.atomic.<sizeof(T)>(CmpXchg, %ptr, %new, %old,
+// ordering)
+void AtomicVisitor::visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) {
+ PointerHelper<AtomicCmpXchgInst> PH(*this, I);
+ checkSizeMatchesType(I, PH.Size, I.getCompareOperand()->getType());
+ checkSizeMatchesType(I, PH.Size, I.getNewValOperand()->getType());
+ replaceWithAtomicIntrinsic(I, PH.PET, PH.Size, NaCl::AtomicCmpXchg, PH.P,
+ I.getNewValOperand(), I.getCompareOperand(),
+ I.getOrdering());
+}
+
+// %res = atomicrmw OP T* %ptr, T %val ordering
+// becomes:
+// %res = call T @llvm.nacl.atomic.<sizeof(T)>(OP, %ptr, %val, 0, ordering)
+void AtomicVisitor::visitAtomicRMWInst(AtomicRMWInst &I) {
+ NaCl::AtomicOperation Op;
+ switch (I.getOperation()) {
+ default:
+ report_fatal_error("unsupported atomicrmw operation: " + ToTwine(I));
+ case AtomicRMWInst::Xchg: Op = NaCl::AtomicXchg; break;
+ case AtomicRMWInst::Add: Op = NaCl::AtomicAdd; break;
+ case AtomicRMWInst::Sub: Op = NaCl::AtomicSub; break;
+ case AtomicRMWInst::And: Op = NaCl::AtomicAnd; break;
+ case AtomicRMWInst::Or: Op = NaCl::AtomicOr; break;
+ case AtomicRMWInst::Xor: Op = NaCl::AtomicXor; break;
+ }
+ PointerHelper<AtomicRMWInst> PH(*this, I);
+ checkSizeMatchesType(I, PH.Size, I.getValOperand()->getType());
+ replaceWithAtomicIntrinsic(I, PH.PET, PH.Size, Op, PH.P, I.getValOperand(),
+ PH.Zero, I.getOrdering());
+}
+
+// fence ordering
+// becomes:
+// call i32 @llvm.nacl.atomic.<sizeof(T)>(Fence, NULL, 0, 0, ordering)
+void AtomicVisitor::visitFenceInst(FenceInst &I) {
+ Type *Int32 = Type::getInt32Ty(C);
+ Value *Zero = ConstantInt::get(Int32, 0);
+ Value *Null = ConstantPointerNull::get(PointerType::getUnqual(Int32));
+ replaceWithAtomicIntrinsic(I, Int32, 32, NaCl::AtomicFence, Null, Zero, Zero,
+ I.getOrdering());
+}
+
+ModulePass *llvm::createRewriteAtomicsPass() { return new RewriteAtomics(); }

Powered by Google App Engine
This is Rietveld 408576698