[llvm] 2b898af - [llvm] Add comment and assert for CloneModule edge case (#67734)

via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 1 10:05:16 PDT 2023


Author: Jacob Lambert
Date: 2023-11-01T10:05:11-07:00
New Revision: 2b898afdef7c8f40e74af9379a1cc7cf372ecb65

URL: https://github.com/llvm/llvm-project/commit/2b898afdef7c8f40e74af9379a1cc7cf372ecb65
DIFF: https://github.com/llvm/llvm-project/commit/2b898afdef7c8f40e74af9379a1cc7cf372ecb65.diff

LOG: [llvm] Add comment and assert for CloneModule edge case (#67734)

CloneModule is not currently designed to handle un-materialized Modules,
for example one created via a lazy initializer like
getLazyBitcodeModule(). In this case we get a somewhat cryptic
segmentation fault without a clear path forward.

In this patch, we add a comment to inform CloneModule users of this
shortcoming, and an assert to test for empty function bodies before the
segmentation fault is triggered.

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/CloneModule.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/CloneModule.cpp b/llvm/lib/Transforms/Utils/CloneModule.cpp
index 55e051298a9a352..aae0a280afeb5c7 100644
--- a/llvm/lib/Transforms/Utils/CloneModule.cpp
+++ b/llvm/lib/Transforms/Utils/CloneModule.cpp
@@ -34,6 +34,8 @@ static void copyComdat(GlobalObject *Dst, const GlobalObject *Src) {
 /// copies of global variables and functions, and making their (initializers and
 /// references, respectively) refer to the right globals.
 ///
+/// Cloning un-materialized modules is not currently supported, so any
+/// modules initialized via lazy loading should be materialized before cloning
 std::unique_ptr<Module> llvm::CloneModule(const Module &M) {
   // Create the value map that maps things from the old module over to the new
   // module.
@@ -49,6 +51,9 @@ std::unique_ptr<Module> llvm::CloneModule(const Module &M,
 std::unique_ptr<Module> llvm::CloneModule(
     const Module &M, ValueToValueMapTy &VMap,
     function_ref<bool(const GlobalValue *)> ShouldCloneDefinition) {
+
+  assert(M.isMaterialized() && "Module must be materialized before cloning!");
+
   // First off, we need to create the new module.
   std::unique_ptr<Module> New =
       std::make_unique<Module>(M.getModuleIdentifier(), M.getContext());


        


More information about the llvm-commits mailing list