From d84b48c1a40395ed751d6a4a061b895ee0f77ee1 Mon Sep 17 00:00:00 2001 From: Prem Chintalapudi Date: Sat, 12 Mar 2022 02:40:30 -0500 Subject: [PATCH] Instrument address space passes and friends --- src/llvm-lower-handlers.cpp | 19 ++++++++++++------- src/llvm-propagate-addrspaces.cpp | 21 +++++++++++++++++---- src/llvm-remove-addrspaces.cpp | 14 +++++++++----- src/llvm-remove-ni.cpp | 2 ++ 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/llvm-lower-handlers.cpp b/src/llvm-lower-handlers.cpp index 747066e731892..21240a96dad1f 100644 --- a/src/llvm-lower-handlers.cpp +++ b/src/llvm-lower-handlers.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -16,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -30,6 +32,8 @@ using namespace llvm; +STATISTIC(ExceptionEnters, "Number of times we enter an exception frame"); +STATISTIC(ExceptionLeaves, "Number of times we leave an exception frame"); /* Lowers Julia Exception Handlers and colors EH frames. * * Our task is to lower: @@ -79,14 +83,12 @@ namespace { */ static void ensure_enter_function(Module &M) { - auto T_int8 = Type::getInt8Ty(M.getContext()); - auto T_pint8 = PointerType::get(T_int8, 0); - auto T_void = Type::getVoidTy(M.getContext()); + auto T_pint8 = Type::getInt8PtrTy(M.getContext()); auto T_int32 = Type::getInt32Ty(M.getContext()); if (!M.getNamedValue(XSTR(jl_enter_handler))) { std::vector ehargs(0); ehargs.push_back(T_pint8); - Function::Create(FunctionType::get(T_void, ehargs, false), + Function::Create(FunctionType::get(Type::getVoidTy(M.getContext()), ehargs, false), Function::ExternalLinkage, XSTR(jl_enter_handler), &M); } if (!M.getNamedValue(jl_setjmp_name)) { @@ -111,7 +113,7 @@ static bool lowerExcHandlers(Function &F) { Function *jlenter_func = M.getFunction(XSTR(jl_enter_handler)); Function *setjmp_func = M.getFunction(jl_setjmp_name); - auto T_pint8 = Type::getInt8PtrTy(M.getContext(), 0); + auto T_pint8 = Type::getInt8PtrTy(M.getContext()); Function *lifetime_start = Intrinsic::getDeclaration(&M, Intrinsic::lifetime_start, { T_pint8 }); Function *lifetime_end = Intrinsic::getDeclaration(&M, Intrinsic::lifetime_end, { T_pint8 }); @@ -143,9 +145,11 @@ static bool lowerExcHandlers(Function &F) { Function *Callee = CI->getCalledFunction(); if (!Callee) continue; - if (Callee == except_enter_func) + if (Callee == except_enter_func) { + ++ExceptionEnters; EnterDepth[CI] = Depth++; - else if (Callee == leave_func) { + } else if (Callee == leave_func) { + ++ExceptionLeaves; LeaveDepth[CI] = Depth; Depth -= cast(CI->getArgOperand(0))->getLimitedValue(); } @@ -215,6 +219,7 @@ static bool lowerExcHandlers(Function &F) { LifetimeEnd->insertAfter(it.first); } } + verifyFunction(F); return true; } diff --git a/src/llvm-propagate-addrspaces.cpp b/src/llvm-propagate-addrspaces.cpp index 8da0e108c94d5..571c0bd252c9d 100644 --- a/src/llvm-propagate-addrspaces.cpp +++ b/src/llvm-propagate-addrspaces.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -28,6 +29,14 @@ #define DEBUG_TYPE "propagate_julia_addrspaces" +STATISTIC(MutatedInsts, "Number of instructions that changed types"); +STATISTIC(LoadInsts, "Number of loads visited"); +STATISTIC(StoreInsts, "Number of stores visited"); +STATISTIC(AtomicCmpXchgInsts, "Number of compare-and-swaps visited"); +STATISTIC(AtomicRMWInsts, "Number of atomic RMWs visited"); +STATISTIC(MemSetInsts, "Number of memsets visited"); +STATISTIC(MemTransferInsts, "Number of memcpys and memmoves visited"); + using namespace llvm; /* This pass performs propagation of addrspace information that is legal from @@ -44,7 +53,6 @@ using namespace llvm; struct PropagateJuliaAddrspacesVisitor : public InstVisitor { DenseMap LiftingMap; SmallPtrSet Visited; - std::vector ToDelete; std::vector> ToInsert; public: @@ -162,6 +170,7 @@ Value *PropagateJuliaAddrspacesVisitor::LiftPointer(Value *V, Instruction *Inser if (LiftingMap.count(V)) continue; if (isa(V) || isa(V) || isa(V)) { + ++MutatedInsts; Instruction *InstV = cast(V); Instruction *NewV = InstV->clone(); ToInsert.push_back(std::make_pair(NewV, InstV)); @@ -229,22 +238,27 @@ void PropagateJuliaAddrspacesVisitor::visitMemop(Instruction &I, Type *T, unsign } void PropagateJuliaAddrspacesVisitor::visitLoadInst(LoadInst &LI) { + ++LoadInsts; visitMemop(LI, LI.getType(), LoadInst::getPointerOperandIndex()); } void PropagateJuliaAddrspacesVisitor::visitStoreInst(StoreInst &SI) { + ++StoreInsts; visitMemop(SI, SI.getValueOperand()->getType(), StoreInst::getPointerOperandIndex()); } void PropagateJuliaAddrspacesVisitor::visitAtomicCmpXchgInst(AtomicCmpXchgInst &SI) { + ++AtomicCmpXchgInsts; visitMemop(SI, SI.getNewValOperand()->getType(), AtomicCmpXchgInst::getPointerOperandIndex()); } void PropagateJuliaAddrspacesVisitor::visitAtomicRMWInst(AtomicRMWInst &SI) { + ++AtomicRMWInsts; visitMemop(SI, SI.getType(), AtomicRMWInst::getPointerOperandIndex()); } void PropagateJuliaAddrspacesVisitor::visitMemSetInst(MemSetInst &MI) { + ++MemSetInsts; unsigned AS = MI.getDestAddressSpace(); if (!isSpecialAS(AS)) return; @@ -258,6 +272,7 @@ void PropagateJuliaAddrspacesVisitor::visitMemSetInst(MemSetInst &MI) { } void PropagateJuliaAddrspacesVisitor::visitMemTransferInst(MemTransferInst &MTI) { + ++MemTransferInsts; unsigned DestAS = MTI.getDestAddressSpace(); unsigned SrcAS = MTI.getSourceAddressSpace(); if (!isSpecialAS(DestAS) && !isSpecialAS(SrcAS)) @@ -289,12 +304,10 @@ bool propagateJuliaAddrspaces(Function &F) { visitor.visit(F); for (auto it : visitor.ToInsert) it.first->insertBefore(it.second); - for (Instruction *I : visitor.ToDelete) - I->eraseFromParent(); visitor.ToInsert.clear(); - visitor.ToDelete.clear(); visitor.LiftingMap.clear(); visitor.Visited.clear(); + verifyFunction(F); return true; } diff --git a/src/llvm-remove-addrspaces.cpp b/src/llvm-remove-addrspaces.cpp index 5cd9a20b8cfd2..c1aa50709624c 100644 --- a/src/llvm-remove-addrspaces.cpp +++ b/src/llvm-remove-addrspaces.cpp @@ -2,6 +2,7 @@ #include "llvm-version.h" +#include #include #include #include @@ -22,7 +23,7 @@ using namespace llvm; using AddrspaceRemapFunction = std::function; - +STATISTIC(JuliaRemappedAddrspaces, "Julia rewritten address spaces"); // // Helpers // @@ -51,7 +52,7 @@ class AddrspaceRemoveTypeRemapper : public ValueMapTypeRemapper { else { //Remove once opaque pointer transition is complete DstTy = PointerType::get( - remapType(Ty->getElementType()), + remapType(Ty->getPointerElementType()), ASRemapper(Ty->getAddressSpace())); } } @@ -161,7 +162,7 @@ class AddrspaceRemoveValueMaterializer : public ValueMaterializer { auto ptrty = cast(Src->getType()->getScalarType()); //Remove once opaque pointer transition is complete if (!ptrty->isOpaque()) { - Type *SrcTy = remapType(ptrty->getElementType()); + Type *SrcTy = remapType(ptrty->getPointerElementType()); DstV = CE->getWithOperands(Ops, Ty, false, SrcTy); } } @@ -452,6 +453,7 @@ bool removeAddrspaces(Module &M, AddrspaceRemapFunction ASRemapper) } } + verifyModule(M); return true; } @@ -499,10 +501,12 @@ PreservedAnalyses RemoveAddrspacesPass::run(Module &M, ModuleAnalysisManager &AM unsigned removeJuliaAddrspaces(unsigned AS) { - if (AddressSpace::FirstSpecial <= AS && AS <= AddressSpace::LastSpecial) + if (AddressSpace::FirstSpecial <= AS && AS <= AddressSpace::LastSpecial) { + ++JuliaRemappedAddrspaces; return AddressSpace::Generic; - else + } else { return AS; + } } struct RemoveJuliaAddrspacesPassLegacy : public ModulePass { diff --git a/src/llvm-remove-ni.cpp b/src/llvm-remove-ni.cpp index c252905dc75f9..823e0e45ae097 100644 --- a/src/llvm-remove-ni.cpp +++ b/src/llvm-remove-ni.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "julia.h" @@ -32,6 +33,7 @@ static bool removeNI(Module &M) } dlstr.erase(nistart, niend - nistart); M.setDataLayout(dlstr); + assert(!verifyModule(M)); return true; } }