Repository navigation
feat(compilerlib): compile to an llvm::Module in the caller's context - #265
Draft
SizzleUnrlsd wants to merge 10 commits into
Draft
SizzleUnrlsd wants to merge 10 commits into
SizzleUnrlsd wants to merge 10 commits into
Conversation
compile() built its context and ran the whole compilation itself. The compilation now runs in runCompilation, from the context it is given, which compileToModule will share. Refs #263
The analyzers compile each source to textual IR or bitcode, then read it back into a module of their own: the module was written and parsed again for nothing. compileToModule(args, context, instrument) gives the module itself, created in the caller's context, with the arguments of an LLVM IR compilation (-S -emit-llvm or -c -emit-llvm). The output file is not written; the other files the arguments ask for, such as a dependency file, are. The module is the one compile() writes, instrumented or not. Code generation sets whether the context discards value names; the context's own setting is restored, since it applies to the modules read into it later, and so is the optimization record that Clang's action leaves on it before LLVM 22. Refs #263
compileToModule gives the module that compile() writes as text, at -O0 and -O2, instrumented or not, and the module of a bitcode compilation, whose output file is not written and whose dependency file is. It fails on a compilation that is not to LLVM IR, reports the errors of a broken source, and leaves the context's setting of value names. Refs #263
With LLVM 16, destroying a module of compileToModule freed memory twice
when the compilation named values and the context, restored after it,
discarded names: LLVM 16's Value::setName("") does nothing in such a
context, so Function::clearArguments leaves each argument's name in
the function's symbol table, which frees it again. LLVM 17 removes the
name. With LLVM 16, a context that discards names now keeps them after
a compilation that names values. The Linux / LLVM 16 job of the CI
aborted in ModuleLeavesTheValueNamesSettingOfTheContext.
Refs #263
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The CoreTrace analyzers compile each source to textual IR or bitcode, then parse it into a module of their own (#263).
compileToModule(args, context, instrument), in a new public headercompilerlib/compile_module.hpp, gives the module itself, created in anllvm::LLVMContextof the host. The output file is not written; the other files the arguments ask for, such as a dependency file, are.Closes #263
Change
refactor(compilerlib): run compile() through runCompilation: the compilation runs from the context it is given.compiledoes not change.feat(compilerlib): compile to an llvm::Module in the caller's context:compileToModuletakes the arguments of an LLVM IR compilation,-S -emit-llvmor-c -emit-llvm, and fails on any other;compilealready uses for in-memory output; withinstrument, the module is instrumented first;test(compilerlib): check the modules compileToModule gives: the module prints as the textcompilegives, at -O0 and -O2, instrumented or not; a bitcode compilation writes its dependency file and no output; a compilation to an object and a broken source fail; the context keeps its setting of value names.docs(readme): describe compiling to a module.fix(compilerlib): keep value names in an LLVM 16 context given some, with its README and changelog notes. The Linux / LLVM 16 job of this pull request aborted inModuleLeavesTheValueNamesSettingOfTheContext:double free or corruption. With LLVM 16,Value::setName("")does nothing in a context that discards names, so destroying a module whose values have names, after the context was set back to discarding them, left each argument's name in its function's symbol table, which freed it again. LLVM 17 removes the name (16.0.6 against 17.0.6,Value::setNameImpl). With LLVM 16, a context that discards names now keeps them after a compilation that names values; the test expects that with LLVM 16, and the restored setting from LLVM 17 on.This branch comes from commit 2c1c83b of the experimental branch, without what it took from the working directory change: #262 brings that separately. The two change
compileincompiler.cpp, so the second to merge needs the other one merged in first.Measurements
coretrace-stack-analyzer db7fb25, with coretrace-compiler built from main (f0a22d9), against the same analyzer taking each module from
compileToModulein its bitcode mode, instead of compiling to a temporary.bcfile, reading it and parsing it. That analyzer change is not published yet: one of its regression tests looks for the log line of the bitcode parse. macOS arm64 (M3, 8 cores), LLVM 20.1.2,--format=json --timing, no IR cache, an empty resource summary cache. Medians of 5 runs, 3 for the 53 sources; the builds alternate within each repetition.--jobs--jobs=1, takes 8 % less time on both sets of C++ sources and 15 % less on Lua's. The analyzer still compiles one source at a time here, whatever--jobssays; feat(compilerlib): give each compilation a working directory of its own #262 is the change that lets it compile in parallel.--jobs=1).Tests
Local runs, on macOS arm64 with AppleClang:
ctest -j2ModuleLeavesTheValueNamesSettingOfTheContextaborts:pointer being freed was not allocated, in~ValueSymbolTablefrom~Module, as on the CI's Linux / LLVM 16 job.Each commit builds and passes the unit tests with LLVM 20. With LLVM 16,
ModuleLeavesTheValueNamesSettingOfTheContextfails from the commit that adds it until the fix. clang-format 18,scripts/check-license-compliance.shand the commit checker pass.CI at 3c715b2
ModuleLeavesTheValueNamesSettingOfTheContextpasses in all 15, Linux / LLVM 16 included, where it aborted at 3dd3255.