Skip to content

Commit 1969bfb

Browse files
committed
refactor: remove runtime prelinking
1 parent 8ba3245 commit 1969bfb

5 files changed

Lines changed: 8 additions & 153 deletions

File tree

‎src/lython/lowering/CMakeLists.txt‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,6 @@ target_include_directories(LythonRuntimeLowering
293293
${CMAKE_CURRENT_SOURCE_DIR}/Passes
294294
)
295295

296-
target_compile_definitions(LythonRuntimeLowering
297-
PRIVATE
298-
LYTHON_RUNTIME_PRELINKED_IR="${CMAKE_BINARY_DIR}/runtime_prelowered.ll"
299-
)
300-
301296
target_link_libraries(LythonRuntimeLowering
302297
PRIVATE
303298
PyDialect

‎src/lython/lowering/Common/RuntimeLibrary.cpp‎

Lines changed: 3 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,8 @@
3030
#include "mlir/Transforms/Passes.h"
3131
#include "llvm/ADT/StringSet.h"
3232
#include "llvm/IR/Module.h"
33-
#include "llvm/IRReader/IRReader.h"
3433
#include "llvm/Linker/Linker.h"
35-
#include "llvm/Support/FileSystem.h"
3634
#include "llvm/Support/MemoryBuffer.h"
37-
#include "llvm/Support/Process.h"
3835
#include "llvm/Support/SourceMgr.h"
3936
#include "llvm/Support/raw_ostream.h"
4037
#include "llvm/TargetParser/Triple.h"
@@ -43,60 +40,16 @@
4340

4441
namespace py::runtime_library {
4542

46-
bool prelowerGenerationMode() {
47-
static const bool enabled = [] {
48-
auto value = llvm::sys::Process::GetEnv("LYTHON_RUNTIME_PRELOWER");
49-
if (!value)
50-
return false;
51-
llvm::StringRef text(*value);
52-
return text == "1" || text.equals_insensitive("true") ||
53-
text.equals_insensitive("yes") || text.equals_insensitive("on");
54-
}();
55-
return enabled;
56-
}
57-
58-
std::optional<std::string> prelinkedRuntimeIRPath() {
59-
if (prelowerGenerationMode())
60-
return std::nullopt;
61-
auto validPrelinkedRuntime = [](llvm::StringRef path) {
62-
auto buffer = llvm::MemoryBuffer::getFile(path);
63-
if (!buffer)
64-
return false;
65-
llvm::StringRef contents = (*buffer)->getBuffer();
66-
return !contents.starts_with("; Runtime prelowering is disabled while "
67-
"PyDialect lowering is rebuilt.");
68-
};
69-
if (auto env = llvm::sys::Process::GetEnv("LYTHON_RUNTIME_PRELINKED_IR")) {
70-
if (env->empty())
71-
return std::nullopt;
72-
if (llvm::sys::fs::exists(*env) && validPrelinkedRuntime(*env))
73-
return *env;
74-
return std::nullopt;
75-
}
76-
#if defined(LYTHON_RUNTIME_PRELINKED_IR)
77-
if (llvm::sys::fs::exists(LYTHON_RUNTIME_PRELINKED_IR) &&
78-
validPrelinkedRuntime(LYTHON_RUNTIME_PRELINKED_IR))
79-
return std::string(LYTHON_RUNTIME_PRELINKED_IR);
80-
#endif
81-
return std::nullopt;
82-
}
83-
8443
namespace {
8544

8645
constexpr llvm::StringLiteral kContractsAttr{"ly.runtime.contracts"};
8746

88-
bool declarationsOnly() { return prelinkedRuntimeIRPath().has_value(); }
89-
9047
bool isContractManifestModule(llvm::StringRef name) {
9148
return name == "typing" || name == "ctypes";
9249
}
9350

9451
bool shouldImportSymbol(mlir::Operation &op) {
95-
if (!mlir::isa<mlir::SymbolOpInterface>(op))
96-
return false;
97-
if (!declarationsOnly())
98-
return true;
99-
return mlir::isa<mlir::FunctionOpInterface>(op);
52+
return mlir::isa<mlir::SymbolOpInterface>(op);
10053
}
10154

10255
bool isFunctionDeclaration(mlir::Operation &op) {
@@ -117,11 +70,9 @@ void importSymbol(mlir::ModuleOp target, mlir::Operation &op) {
11770

11871
mlir::OpBuilder builder(target.getContext());
11972
builder.setInsertionPointToEnd(target.getBody());
120-
mlir::Operation *cloned =
121-
declarationsOnly() ? builder.cloneWithoutRegions(op) : builder.clone(op);
73+
mlir::Operation *cloned = builder.clone(op);
12274
if (auto clonedSymbol = mlir::dyn_cast<mlir::SymbolOpInterface>(cloned)) {
123-
if (!prelowerGenerationMode())
124-
clonedSymbol.setVisibility(mlir::SymbolTable::Visibility::Private);
75+
clonedSymbol.setVisibility(mlir::SymbolTable::Visibility::Private);
12576
}
12677
}
12778

@@ -202,28 +153,6 @@ mlir::LogicalResult embedObjectModules(mlir::ModuleOp module) {
202153
return mlir::success();
203154
}
204155

205-
mlir::LogicalResult linkPrelinkedRuntime(llvm::Module &llvmModule) {
206-
std::optional<std::string> path = prelinkedRuntimeIRPath();
207-
if (!path)
208-
return mlir::success();
209-
llvm::SMDiagnostic diagnostic;
210-
std::unique_ptr<llvm::Module> runtime =
211-
llvm::parseIRFile(*path, diagnostic, llvmModule.getContext());
212-
if (!runtime) {
213-
llvm::errs() << "error: failed to load pre-lowered runtime IR from '"
214-
<< *path << "': " << diagnostic.getMessage() << "\n";
215-
return mlir::failure();
216-
}
217-
runtime->setDataLayout(llvmModule.getDataLayout());
218-
runtime->setTargetTriple(llvmModule.getTargetTriple());
219-
if (llvm::Linker::linkModules(llvmModule, std::move(runtime),
220-
llvm::Linker::Flags::LinkOnlyNeeded)) {
221-
llvm::errs() << "error: failed to link pre-lowered runtime IR\n";
222-
return mlir::failure();
223-
}
224-
return mlir::success();
225-
}
226-
227156
namespace {
228157

229158
bool isPlatformNativeSupport(llvm::StringRef name) {

‎src/lython/lowering/Common/RuntimeLibrary.h‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,13 @@
33
#include "mlir/IR/BuiltinOps.h"
44
#include "mlir/Support/LogicalResult.h"
55

6-
#include <optional>
7-
#include <string>
8-
96
namespace llvm {
107
class Module;
118
}
129

1310
namespace py::runtime_library {
1411

1512
mlir::LogicalResult embedObjectModules(mlir::ModuleOp module);
16-
mlir::LogicalResult linkPrelinkedRuntime(llvm::Module &llvmModule);
1713
mlir::LogicalResult linkEmbeddedNativeRuntime(llvm::Module &llvmModule);
1814

19-
bool prelowerGenerationMode();
20-
std::optional<std::string> prelinkedRuntimeIRPath();
21-
2215
} // namespace py::runtime_library

‎tools/CLI.cpp‎

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -714,12 +714,6 @@ enum class LLVMSafetyContractCoverage {
714714
static constexpr llvm::StringLiteral kLythonSafetyMetadataName{"ly.safety"};
715715
static constexpr llvm::StringLiteral kLythonSafetyMetadataVersion{
716716
"ly.safety.v1"};
717-
// Atomics inside the pre-lowered runtime cache were verified by the full
718-
// pipeline when the cache was generated at build time. Their metadata is
719-
// sealed to this version so the per-compile verifier can recognize them
720-
// without confusing their stale contract ids with the current profile.
721-
static constexpr llvm::StringLiteral kLythonRuntimeSafetyMetadataVersion{
722-
"ly.safety.runtime.v1"};
723717
static constexpr llvm::StringLiteral kPySafetyContractIdAttr{
724718
"py.safety_contract_id"};
725719

@@ -982,21 +976,12 @@ std::optional<int64_t> getLythonSafetyMetadataId(llvm::Instruction &inst) {
982976
return intConstant->getSExtValue();
983977
}
984978

985-
bool hasRuntimeVerifiedSafetyMetadata(llvm::Instruction &inst) {
986-
llvm::MDNode *node = inst.getMetadata(kLythonSafetyMetadataName);
987-
if (!node || node->getNumOperands() < 1)
988-
return false;
989-
auto *version = llvm::dyn_cast<llvm::MDString>(node->getOperand(0));
990-
return version && version->getString() == kLythonRuntimeSafetyMetadataVersion;
991-
}
992-
993979
void collectLinkedLLVMSafetyContracts(llvm::Module &llvmModule,
994980
LLVMSafetyProfile &profile) {
995981
for (llvm::Function &function : llvmModule) {
996982
for (llvm::BasicBlock &block : function) {
997983
for (llvm::Instruction &inst : block) {
998-
if (hasRuntimeVerifiedSafetyMetadata(inst) ||
999-
getLythonSafetyMetadataId(inst))
984+
if (getLythonSafetyMetadataId(inst))
1000985
continue;
1001986

1002987
LLVMSafetyContract contract;
@@ -1031,21 +1016,6 @@ void collectLinkedLLVMSafetyContracts(llvm::Module &llvmModule,
10311016
}
10321017
}
10331018

1034-
// Rewrites the safety metadata of every contracted instruction to the
1035-
// runtime-verified version. Called only by the prelower generation run after
1036-
// all verifications have passed; the resulting IR is the build-time cache.
1037-
void sealRuntimeSafetyMetadata(llvm::Module &llvmModule) {
1038-
llvm::LLVMContext &ctx = llvmModule.getContext();
1039-
llvm::Metadata *operands[] = {
1040-
llvm::MDString::get(ctx, kLythonRuntimeSafetyMetadataVersion)};
1041-
llvm::MDNode *sealed = llvm::MDNode::get(ctx, operands);
1042-
for (llvm::Function &function : llvmModule)
1043-
for (llvm::BasicBlock &block : function)
1044-
for (llvm::Instruction &inst : block)
1045-
if (getLythonSafetyMetadataId(inst))
1046-
inst.setMetadata(kLythonSafetyMetadataName, sealed);
1047-
}
1048-
10491019
class PySafetyLLVMIRTranslationInterface final
10501020
: public LLVMTranslationDialectInterface {
10511021
public:
@@ -1095,10 +1065,6 @@ LogicalResult verifyLLVMIRSafetyMetadataPreserved(
10951065
for (llvm::Function &function : llvmModule) {
10961066
for (llvm::BasicBlock &block : function) {
10971067
for (llvm::Instruction &inst : block) {
1098-
// Instructions from the pre-lowered runtime cache carry sealed
1099-
// metadata: their contracts were verified when the cache was built.
1100-
if (hasRuntimeVerifiedSafetyMetadata(inst))
1101-
continue;
11021068
auto id = getLythonSafetyMetadataId(inst);
11031069
if (!id) {
11041070
if (auto recovered =
@@ -1679,8 +1645,6 @@ LogicalResult runJIT(ModuleOp module, const py::IRDumpConfig &irDump,
16791645

16801646
llvmModule->setDataLayout(jit->getDataLayout());
16811647
llvmModule->setTargetTriple(jit->getTargetTriple().getTriple());
1682-
if (failed(py::runtime_library::linkPrelinkedRuntime(*llvmModule)))
1683-
return failure();
16841648
if (failed(py::runtime_library::linkEmbeddedNativeRuntime(*llvmModule)))
16851649
return failure();
16861650
collectLinkedLLVMSafetyContracts(*llvmModule, safetyProfile);
@@ -1964,22 +1928,14 @@ int main(int argc, char **argv) {
19641928
if (failed(configureLLVMModuleCodeGenTarget(*llvmModule, tensorTarget)))
19651929
return 1;
19661930

1967-
if (py::runtime_library::prelowerGenerationMode()) {
1968-
sealRuntimeSafetyMetadata(*llvmModule);
1969-
} else {
1970-
if (failed(py::runtime_library::linkPrelinkedRuntime(*llvmModule)))
1971-
return 1;
1972-
if (failed(py::runtime_library::linkEmbeddedNativeRuntime(*llvmModule)))
1973-
return 1;
1974-
}
1931+
if (failed(py::runtime_library::linkEmbeddedNativeRuntime(*llvmModule)))
1932+
return 1;
19751933
rewriteExceptionPersonalityForTarget(*llvmModule);
19761934

1977-
if (!py::runtime_library::prelowerGenerationMode() &&
1978-
failed(installAOTEntryPoint(*llvmModule)))
1935+
if (failed(installAOTEntryPoint(*llvmModule)))
19791936
return 1;
19801937

1981-
if (!py::runtime_library::prelowerGenerationMode())
1982-
collectLinkedLLVMSafetyContracts(*llvmModule, safetyProfile);
1938+
collectLinkedLLVMSafetyContracts(*llvmModule, safetyProfile);
19831939

19841940
if (EmitLLVMOnly)
19851941
return failed(writeLLVMIR(*llvmModule, outputPath)) ? 1 : 0;

‎tools/CMakeLists.txt‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,3 @@ endif()
3939
set_target_properties(lyc PROPERTIES
4040
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
4141
)
42-
43-
# Pre-lowered runtime cache: lyc compiles an empty module, which embeds and
44-
# fully lowers (and verifies) every runtime MLIR object; the emitted LLVM IR
45-
# is then linked into each subsequent compilation instead of re-lowering the
46-
# runtime per compile. LYTHON_RUNTIME_PRELOWER=1 keeps the embedded symbols
47-
# public and seals their safety metadata as build-time verified.
48-
set(LYTHON_RUNTIME_PRELOWERED_IR "${CMAKE_BINARY_DIR}/runtime_prelowered.ll")
49-
set(LYTHON_PRELOWER_INPUT "${CMAKE_BINARY_DIR}/lython_prelower_input.mlir")
50-
file(WRITE "${LYTHON_PRELOWER_INPUT}" "module {}\n")
51-
file(GLOB LYTHON_PRELOWER_RUNTIME_SOURCES CONFIGURE_DEPENDS
52-
"${CMAKE_SOURCE_DIR}/src/lython/runtime/objects/*.mlir"
53-
)
54-
file(WRITE "${LYTHON_RUNTIME_PRELOWERED_IR}"
55-
"; Runtime prelowering is disabled while PyDialect lowering is rebuilt.\n"
56-
)
57-
add_custom_target(LythonRuntimePrelowered ALL
58-
DEPENDS "${LYTHON_RUNTIME_PRELOWERED_IR}"
59-
)

0 commit comments

Comments
 (0)