public:
virtual ~GVMaterializer();
- /// True if GV has been materialized and can be dematerialized back to
- /// whatever backing store this GVMaterializer uses.
- virtual bool isDematerializable(const GlobalValue *GV) const = 0;
-
/// Make sure the given GlobalValue is fully read.
///
virtual std::error_code materialize(GlobalValue *GV) = 0;
- /// If the given GlobalValue is read in, and if the GVMaterializer supports
- /// it, release the memory for the GV, and set it up to be materialized
- /// lazily. If the Materializer doesn't support this capability, this method
- /// is a noop.
- ///
- virtual void dematerialize(GlobalValue *) {}
-
/// Make sure the entire Module has been completely read.
///
virtual std::error_code materializeModule(Module *M) = 0;
/// function has been read in yet or not.
bool isMaterializable() const;
- /// Returns true if this function was loaded from a GVMaterializer that's
- /// still attached to its Module and that knows how to dematerialize the
- /// function.
- bool isDematerializable() const;
-
/// Make sure this GlobalValue is fully read. If the module is corrupt, this
/// returns true and fills in the optional string with information about the
/// problem. If successful, this returns false.
std::error_code materialize();
- /// If this GlobalValue is read in, and if the GVMaterializer supports it,
- /// release the memory for the function, and set it up to be materialized
- /// lazily. If !isDematerializable(), this method is a noop.
- void dematerialize();
-
/// @}
/// Return true if the primary definition of this global value is outside of
/// Retrieves the GVMaterializer, if any, for this Module.
GVMaterializer *getMaterializer() const { return Materializer.get(); }
- /// Returns true if this GV was loaded from this Module's GVMaterializer and
- /// the GVMaterializer knows how to dematerialize the GV.
- bool isDematerializable(const GlobalValue *GV) const;
-
/// Make sure the GlobalValue is fully read. If the module is corrupt, this
/// returns true and fills in the optional string with information about the
/// problem. If successful, this returns false.
std::error_code materialize(GlobalValue *GV);
- /// If the GlobalValue is read in, and if the GVMaterializer supports it,
- /// release the memory for the function, and set it up to be materialized
- /// lazily. If !isDematerializable(), this method is a no-op.
- void dematerialize(GlobalValue *GV);
/// Make sure all GlobalValues in this Module are fully read.
std::error_code materializeAll();
/// (e.g.) blockaddress forward references.
bool WillMaterializeAllForwardRefs = false;
- /// Functions that have block addresses taken. This is usually empty.
- SmallPtrSet<const Function *, 4> BlockAddressesTaken;
-
/// True if any Metadata block has been materialized.
bool IsMetadataMaterialized = false;
void releaseBuffer();
- bool isDematerializable(const GlobalValue *GV) const override;
std::error_code materialize(GlobalValue *GV) override;
std::error_code materializeModule(Module *M) override;
std::vector<StructType *> getIdentifiedStructTypes() const override;
- void dematerialize(GlobalValue *GV) override;
/// \brief Main interface to parsing a bitcode buffer.
/// \returns true if an error occurred.
if (!Fn)
return error("Invalid record");
- // Don't let Fn get dematerialized.
- BlockAddressesTaken.insert(Fn);
-
// If the function is already parsed we can insert the block address right
// away.
BasicBlock *BB;
return materializeForwardReferencedFunctions();
}
-bool BitcodeReader::isDematerializable(const GlobalValue *GV) const {
- const Function *F = dyn_cast<Function>(GV);
- if (!F || F->isDeclaration())
- return false;
-
- // Dematerializing F would leave dangling references that wouldn't be
- // reconnected on re-materialization.
- if (BlockAddressesTaken.count(F))
- return false;
-
- return DeferredFunctionInfo.count(const_cast<Function*>(F));
-}
-
-void BitcodeReader::dematerialize(GlobalValue *GV) {
- Function *F = dyn_cast<Function>(GV);
- // If this function isn't dematerializable, this is a noop.
- if (!F || !isDematerializable(F))
- return;
-
- assert(DeferredFunctionInfo.count(F) && "No info to read function later?");
-
- // Just forget the function body, we can remat it later.
- F->dropAllReferences();
- F->setIsMaterializable(true);
-}
-
std::error_code BitcodeReader::materializeModule(Module *M) {
assert(M == TheModule &&
"Can only Materialize the Module this BitcodeReader is attached to.");
return F->isMaterializable();
return false;
}
-bool GlobalValue::isDematerializable() const {
- return getParent() && getParent()->isDematerializable(this);
-}
std::error_code GlobalValue::materialize() {
return getParent()->materialize(this);
}
-void GlobalValue::dematerialize() {
- getParent()->dematerialize(this);
-}
/// Override destroyConstantImpl to make sure it doesn't get called on
/// GlobalValue's because they shouldn't be treated like other constants.
Materializer.reset(GVM);
}
-bool Module::isDematerializable(const GlobalValue *GV) const {
- if (Materializer)
- return Materializer->isDematerializable(GV);
- return false;
-}
-
std::error_code Module::materialize(GlobalValue *GV) {
if (!Materializer)
return std::error_code();
return Materializer->materialize(GV);
}
-void Module::dematerialize(GlobalValue *GV) {
- if (Materializer)
- return Materializer->dematerialize(GV);
-}
-
std::error_code Module::materializeAll() {
if (!Materializer)
return std::error_code();
for (Argument &Arg : Src.args())
ValueMap.erase(&Arg);
- Src.dematerialize();
return false;
}
EXPECT_FALSE(M->getFunction("func")->empty());
}
-TEST(BitReaderTest, DematerializeFunctionPreservesLinkageType) {
- SmallString<1024> Mem;
-
- LLVMContext Context;
- std::unique_ptr<Module> M = getLazyModuleFromAssembly(
- Context, Mem, "define internal i32 @func() {\n"
- "ret i32 0\n"
- "}\n");
-
- EXPECT_FALSE(verifyModule(*M, &dbgs()));
-
- M->getFunction("func")->materialize();
- EXPECT_FALSE(M->getFunction("func")->empty());
- EXPECT_TRUE(M->getFunction("func")->getLinkage() ==
- GlobalValue::InternalLinkage);
-
- // Check that the linkage type is preserved after dematerialization.
- M->getFunction("func")->dematerialize();
- EXPECT_TRUE(M->getFunction("func")->empty());
- EXPECT_TRUE(M->getFunction("func")->getLinkage() ==
- GlobalValue::InternalLinkage);
- EXPECT_FALSE(verifyModule(*M, &dbgs()));
-}
-
// Tests that lazy evaluation can parse functions out of order.
TEST(BitReaderTest, MaterializeFunctionsOutOfOrder) {
SmallString<1024> Mem;
" unreachable\n"
"}\n");
EXPECT_FALSE(verifyModule(*M, &dbgs()));
-
- // Try (and fail) to dematerialize @func.
- M->getFunction("func")->dematerialize();
- EXPECT_FALSE(M->getFunction("func")->empty());
}
TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionBefore) {
EXPECT_FALSE(M->getFunction("func")->empty());
EXPECT_TRUE(M->getFunction("other")->empty());
EXPECT_FALSE(verifyModule(*M, &dbgs()));
-
- // Try (and fail) to dematerialize @func.
- M->getFunction("func")->dematerialize();
- EXPECT_FALSE(M->getFunction("func")->empty());
- EXPECT_FALSE(verifyModule(*M, &dbgs()));
}
TEST(BitReaderTest, MaterializeFunctionsForBlockAddrInFunctionAfter) {
EXPECT_FALSE(M->getFunction("func")->empty());
EXPECT_TRUE(M->getFunction("other")->empty());
EXPECT_FALSE(verifyModule(*M, &dbgs()));
-
- // Try (and fail) to dematerialize @func.
- M->getFunction("func")->dematerialize();
- EXPECT_FALSE(M->getFunction("func")->empty());
- EXPECT_FALSE(verifyModule(*M, &dbgs()));
}
} // end namespace