From 483cb93b22f5bd139f0164f6d4ce045115678d3a Mon Sep 17 00:00:00 2001 From: Yi Lin Date: Thu, 28 Nov 2024 03:38:03 +0000 Subject: [PATCH] Refactor LateLowerGCFrame. We do not need inheritance. --- src/llvm-gc-interface-passes.h | 17 ++++---- src/llvm-julia-passes.inc | 2 +- src/llvm-late-gc-lowering-mmtk.cpp | 47 ++++++++++++++++++++-- src/llvm-late-gc-lowering-stock.cpp | 5 +-- src/llvm-late-gc-lowering.cpp | 60 ++--------------------------- 5 files changed, 61 insertions(+), 70 deletions(-) diff --git a/src/llvm-gc-interface-passes.h b/src/llvm-gc-interface-passes.h index 3c3aa4afa5828..b58f12fe255c6 100644 --- a/src/llvm-gc-interface-passes.h +++ b/src/llvm-gc-interface-passes.h @@ -326,8 +326,7 @@ struct LateLowerGCFrame: private JuliaPassContext { LateLowerGCFrame(function_ref GetDT) : GetDT(GetDT) {} public: - int need_gc_preserve_hook; - virtual bool runOnFunction(Function &F, bool *CFGModified = nullptr); + bool runOnFunction(Function &F, bool *CFGModified = nullptr); private: CallInst *pgcstack; @@ -362,6 +361,7 @@ struct LateLowerGCFrame: private JuliaPassContext { void PlaceGCFrameStores(State &S, unsigned MinColorRoot, ArrayRef Colors, Value *GCFrame); void PlaceRootsAndUpdateCalls(SmallVectorImpl &Colors, State &S, std::map>); void CleanupWriteBarriers(Function &F, State *S, const SmallVector &WriteBarriers, bool *CFGModified); + void cleanupGCPreserve(Function &F, CallInst *CI, Value *callee, Type *T_size); bool CleanupIR(Function &F, State *S, bool *CFGModified); void NoteUseChain(State &S, BBState &BBS, User *TheUser); SmallVector GetPHIRefinements(PHINode *phi, State &S); @@ -374,11 +374,6 @@ struct LateLowerGCFrame: private JuliaPassContext { #endif }; -struct LateLowerGCFrameCustom: public LateLowerGCFrame { -public: - bool runOnFunction(Function &F, bool *CFGModified = nullptr) override; -}; - // The final GC lowering pass. This pass lowers platform-agnostic GC // intrinsics to platform-dependent instruction sequences. The // intrinsics it targets are those produced by the late GC frame @@ -434,4 +429,12 @@ struct FinalLowerGC: private JuliaPassContext { #endif }; +inline bool isSpecialPtr(Type *Ty) { + PointerType *PTy = dyn_cast(Ty); + if (!PTy) + return false; + unsigned AS = PTy->getAddressSpace(); + return AddressSpace::FirstSpecial <= AS && AS <= AddressSpace::LastSpecial; +} + #endif // LLVM_GC_PASSES_H diff --git a/src/llvm-julia-passes.inc b/src/llvm-julia-passes.inc index 5d2c0daef00a9..c41ecbba87b6a 100644 --- a/src/llvm-julia-passes.inc +++ b/src/llvm-julia-passes.inc @@ -11,7 +11,7 @@ MODULE_PASS("LowerPTLSPass", LowerPTLSPass, LowerPTLSPass()) //Function passes #ifdef FUNCTION_PASS FUNCTION_PASS("DemoteFloat16", DemoteFloat16Pass, DemoteFloat16Pass()) -FUNCTION_PASS("LateLowerGCFrameCustom", LateLowerGCPass, LateLowerGCPass()) +FUNCTION_PASS("LateLowerGCFrame", LateLowerGCPass, LateLowerGCPass()) FUNCTION_PASS("AllocOpt", AllocOptPass, AllocOptPass()) FUNCTION_PASS("PropagateJuliaAddrspaces", PropagateJuliaAddrspacesPass, PropagateJuliaAddrspacesPass()) FUNCTION_PASS("LowerExcHandlers", LowerExcHandlersPass, LowerExcHandlersPass()) diff --git a/src/llvm-late-gc-lowering-mmtk.cpp b/src/llvm-late-gc-lowering-mmtk.cpp index dec1cbcd052e2..057962cc23b82 100644 --- a/src/llvm-late-gc-lowering-mmtk.cpp +++ b/src/llvm-late-gc-lowering-mmtk.cpp @@ -1,6 +1,47 @@ #include "llvm-gc-interface-passes.h" -bool LateLowerGCFrameCustom::runOnFunction(Function &F, bool *CFGModified) { - need_gc_preserve_hook = 1; - return LateLowerGCFrame::runOnFunction(F, CFGModified); +void LateLowerGCFrame::cleanupGCPreserve(Function &F, CallInst *CI, Value *callee, Type *T_size) { + if (callee == gc_preserve_begin_func) { + // Initialize an IR builder. + IRBuilder<> builder(CI); + + builder.SetCurrentDebugLocation(CI->getDebugLoc()); + size_t nargs = 0; + State S2(F); + + std::vector args; + for (Use &U : CI->args()) { + Value *V = U; + if (isa(V)) + continue; + if (isa(V->getType())) { + if (isSpecialPtr(V->getType())) { + int Num = Number(S2, V); + if (Num >= 0) { + nargs++; + Value *Val = GetPtrForNumber(S2, Num, CI); + args.push_back(Val); + } + } + } else { + auto Nums = NumberAll(S2, V); + for (int Num : Nums) { + if (Num < 0) + continue; + Value *Val = GetPtrForNumber(S2, Num, CI); + args.push_back(Val); + nargs++; + } + } + } + args.insert(args.begin(), ConstantInt::get(T_size, nargs)); + + ArrayRef args_llvm = ArrayRef(args); + builder.CreateCall(getOrDeclare(jl_well_known::GCPreserveBeginHook), args_llvm ); + } else if (callee == gc_preserve_end_func) { + // Initialize an IR builder. + IRBuilder<> builder(CI); + builder.SetCurrentDebugLocation(CI->getDebugLoc()); + builder.CreateCall(getOrDeclare(jl_well_known::GCPreserveEndHook), {}); + } } diff --git a/src/llvm-late-gc-lowering-stock.cpp b/src/llvm-late-gc-lowering-stock.cpp index 7e7d145bccc87..b8f4065b06f59 100644 --- a/src/llvm-late-gc-lowering-stock.cpp +++ b/src/llvm-late-gc-lowering-stock.cpp @@ -1,6 +1,5 @@ #include "llvm-gc-interface-passes.h" -bool LateLowerGCFrameCustom::runOnFunction(Function &F, bool *CFGModified) { - need_gc_preserve_hook = 0; - return LateLowerGCFrame::runOnFunction(F, CFGModified); +void LateLowerGCFrame::cleanupGCPreserve(Function &F, CallInst *CI, Value *callee, Type *T_size) { + // Do nothing for the stock GC } diff --git a/src/llvm-late-gc-lowering.cpp b/src/llvm-late-gc-lowering.cpp index d025a6f8c041a..2e41ebe4b5023 100644 --- a/src/llvm-late-gc-lowering.cpp +++ b/src/llvm-late-gc-lowering.cpp @@ -13,14 +13,6 @@ static bool isTrackedValue(Value *V) { return PT && PT->getAddressSpace() == AddressSpace::Tracked; } -static bool isSpecialPtr(Type *Ty) { - PointerType *PTy = dyn_cast(Ty); - if (!PTy) - return false; - unsigned AS = PTy->getAddressSpace(); - return AddressSpace::FirstSpecial <= AS && AS <= AddressSpace::LastSpecial; -} - // return how many Special pointers are in T (count > 0), // and if there is anything else in T (all == false) CountTrackedPointers::CountTrackedPointers(Type *T, bool ignore_loaded) { @@ -2089,55 +2081,11 @@ bool LateLowerGCFrame::CleanupIR(Function &F, State *S, bool *CFGModified) { continue; } Value *callee = CI->getCalledOperand(); - if (callee && (callee == gc_flush_func || callee == gc_preserve_begin_func + if (callee && callee == gc_flush_func) { + /* No replacement */ + } else if (callee && (callee == gc_preserve_begin_func || callee == gc_preserve_end_func)) { - /* Replace with a call to the hook functions */ - if (need_gc_preserve_hook) { - if (callee == gc_preserve_begin_func) { - // Initialize an IR builder. - IRBuilder<> builder(CI); - - builder.SetCurrentDebugLocation(CI->getDebugLoc()); - size_t nargs = 0; - State S2(F); - - std::vector args; - for (Use &U : CI->args()) { - Value *V = U; - if (isa(V)) - continue; - if (isa(V->getType())) { - if (isSpecialPtr(V->getType())) { - int Num = Number(S2, V); - if (Num >= 0) { - nargs++; - Value *Val = GetPtrForNumber(S2, Num, CI); - args.push_back(Val); - } - } - } else { - auto Nums = NumberAll(S2, V); - for (int Num : Nums) { - if (Num < 0) - continue; - Value *Val = GetPtrForNumber(S2, Num, CI); - args.push_back(Val); - nargs++; - } - } - } - args.insert(args.begin(), ConstantInt::get(T_size, nargs)); - - ArrayRef args_llvm = ArrayRef(args); - builder.CreateCall(getOrDeclare(jl_well_known::GCPreserveBeginHook), args_llvm ); - } else if (callee == gc_preserve_end_func) { - // Initialize an IR builder. - IRBuilder<> builder(CI); - builder.SetCurrentDebugLocation(CI->getDebugLoc()); - builder.CreateCall(getOrDeclare(jl_well_known::GCPreserveEndHook), {}); - } - } - // Otherwise no replacement + cleanupGCPreserve(F, CI, callee, T_size); } else if (pointer_from_objref_func != nullptr && callee == pointer_from_objref_func) { auto *obj = CI->getOperand(0); auto *ASCI = new AddrSpaceCastInst(obj, CI->getType(), "", CI);