Skip to content
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

Instrument address space passes and friends #44579

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/llvm-lower-handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <llvm-c/Types.h>

#include <llvm/ADT/DepthFirstIterator.h>
#include <llvm/ADT/Statistic.h>
#include <llvm/Analysis/CFG.h>
#include <llvm/IR/BasicBlock.h>
#include <llvm/IR/Constants.h>
Expand All @@ -16,6 +17,7 @@
#include <llvm/IR/Module.h>
#include <llvm/IR/Value.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/IR/Verifier.h>
#include <llvm/Pass.h>
#include <llvm/Support/Debug.h>
#include <llvm/Transforms/Utils/BasicBlockUtils.h>
Expand All @@ -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:
Expand Down Expand Up @@ -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<Type*> 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)) {
Expand All @@ -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 });

Expand Down Expand Up @@ -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<ConstantInt>(CI->getArgOperand(0))->getLimitedValue();
}
Expand Down Expand Up @@ -215,6 +219,7 @@ static bool lowerExcHandlers(Function &F) {
LifetimeEnd->insertAfter(it.first);
}
}
verifyFunction(F);
return true;
}

Expand Down
21 changes: 17 additions & 4 deletions src/llvm-propagate-addrspaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <llvm-c/Types.h>

#include <llvm/ADT/SmallPtrSet.h>
#include <llvm/ADT/Statistic.h>
#include <llvm/Analysis/CFG.h>
#include <llvm/IR/Value.h>
#include <llvm/IR/ValueMap.h>
Expand All @@ -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
Expand All @@ -44,7 +53,6 @@ using namespace llvm;
struct PropagateJuliaAddrspacesVisitor : public InstVisitor<PropagateJuliaAddrspacesVisitor> {
DenseMap<Value *, Value *> LiftingMap;
SmallPtrSet<Value *, 4> Visited;
std::vector<Instruction *> ToDelete;
std::vector<std::pair<Instruction *, Instruction *>> ToInsert;

public:
Expand Down Expand Up @@ -162,6 +170,7 @@ Value *PropagateJuliaAddrspacesVisitor::LiftPointer(Value *V, Instruction *Inser
if (LiftingMap.count(V))
continue;
if (isa<GetElementPtrInst>(V) || isa<PHINode>(V) || isa<SelectInst>(V)) {
++MutatedInsts;
Instruction *InstV = cast<Instruction>(V);
Instruction *NewV = InstV->clone();
ToInsert.push_back(std::make_pair(NewV, InstV));
Expand Down Expand Up @@ -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;
Expand All @@ -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))
Expand Down Expand Up @@ -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;
}

Expand Down
14 changes: 9 additions & 5 deletions src/llvm-remove-addrspaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "llvm-version.h"

#include <llvm/ADT/Statistic.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Verifier.h>
#include <llvm/IR/Constants.h>
Expand All @@ -22,7 +23,7 @@ using namespace llvm;

using AddrspaceRemapFunction = std::function<unsigned(unsigned)>;


STATISTIC(JuliaRemappedAddrspaces, "Julia rewritten address spaces");
//
// Helpers
//
Expand Down Expand Up @@ -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()));
}
}
Expand Down Expand Up @@ -161,7 +162,7 @@ class AddrspaceRemoveValueMaterializer : public ValueMaterializer {
auto ptrty = cast<PointerType>(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);
}
}
Expand Down Expand Up @@ -452,6 +453,7 @@ bool removeAddrspaces(Module &M, AddrspaceRemapFunction ASRemapper)
}
}

verifyModule(M);
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
verifyModule(M);

return true;
}

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/llvm-remove-ni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <llvm/IR/Module.h>
#include <llvm/IR/PassManager.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/IR/Verifier.h>
#include <llvm/Support/Debug.h>

#include "julia.h"
Expand All @@ -32,6 +33,7 @@ static bool removeNI(Module &M)
}
dlstr.erase(nistart, niend - nistart);
M.setDataLayout(dlstr);
assert(!verifyModule(M));
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert(!verifyModule(M));

return true;
}
}
Expand Down