-
Notifications
You must be signed in to change notification settings - Fork 15.5k
[RFC] Initial reference pass-plugin in LLVM #171111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
weliveindetail
wants to merge
9
commits into
llvm:main
Choose a base branch
from
weliveindetail:pypass-plugin-dev
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,011
−8
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0d26c1e
[RFC] Initial reference pass-plugin in LLVM
weliveindetail 015dd38
Drop non-Ex variants of Py_Initialize/Py_Finalize
weliveindetail 2a734bd
Turn getPyAPI() into static PythonAPI::instance()
weliveindetail c227080
Add TODO that considers multi-script support
weliveindetail 4e44075
Print error message if loadScript() fails
weliveindetail 5a0f5bd
Check PyAPI state early and rename flag Ready -> Valid
weliveindetail 817cdad
Replace PyRun_SimpleString() with exec() builtin and stable-API funct…
weliveindetail 2bb30ce
clang-format
weliveindetail 4bbe57d
Add tests for plugin load failures
weliveindetail File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| void f() {} | ||
| void g() {} |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import os | ||
| import platform | ||
| import sysconfig | ||
|
|
||
| # Run end-to-end tests if the reference pass-plugin exists in LLVM | ||
| pypass_plugin = f"pypass-plugin{config.llvm_plugin_ext}" | ||
| pypass_plugin_shlib = os.path.join(config.llvm_libs_dir, pypass_plugin) | ||
| if os.path.exists(pypass_plugin_shlib): | ||
| config.available_features.add("pypass-plugin") | ||
|
|
||
| # Disable ASAN's leak detection for Python tests | ||
| config.environment["ASAN_OPTIONS"] = "detect_leaks=0" | ||
|
|
||
| if platform.system() != "Windows": | ||
| libdir = sysconfig.get_config_var("LIBDIR") | ||
| dylib = sysconfig.get_config_var("LDLIBRARY") | ||
| config.substitutions.append(("%libpython", os.path.join(libdir, dylib))) | ||
|
|
||
| # FIXME: %llvmshlibdir is broken in standalone builds | ||
| config.substitutions.append(("%plugindir", config.llvm_libs_dir)) | ||
| config.suffixes.add(".py") |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| # REQUIRES: native, system-linux, plugins, pypass-plugin | ||
|
|
||
| # Entry-points in default and -O0 pipeline | ||
| # | ||
| # RUN: env LLVM_PYPASS_SCRIPT=%s \ | ||
| # RUN: env LLVM_PYPASS_DYLIB=%libpython \ | ||
| # RUN: %clang -fpass-plugin=%plugindir/pypass-plugin%pluginext \ | ||
| # RUN: -o /dev/null -S -emit-llvm %S/Inputs/pypass-plugin.c | FileCheck --check-prefix=EP %s | ||
| # | ||
| # RUN: env LLVM_PYPASS_SCRIPT=%s \ | ||
| # RUN: env LLVM_PYPASS_DYLIB=%libpython \ | ||
| # RUN: %clang -fpass-plugin=%plugindir/pypass-plugin%pluginext -flto=full -O0 \ | ||
| # RUN: -o /dev/null -S -emit-llvm %S/Inputs/pypass-plugin.c | FileCheck --check-prefix=EP %s | ||
| # | ||
| # RUN: env LLVM_PYPASS_SCRIPT=%s \ | ||
| # RUN: env LLVM_PYPASS_DYLIB=%libpython \ | ||
| # RUN: %clang -fpass-plugin=%plugindir/pypass-plugin%pluginext -flto=thin -O0 \ | ||
| # RUN: -o /dev/null -S -emit-llvm %S/Inputs/pypass-plugin.c | FileCheck --check-prefix=EP %s | ||
| # | ||
| # EP-NOT: PeepholeEPCallback | ||
| # EP-NOT: Optimizer{{.*}}EPCallback | ||
| # EP-NOT: ScalarOptimizer{{.*}}EPCallback | ||
| # EP-NOT: FullLinkTimeOptimization{{.*}}EPCallback | ||
| # | ||
| # EP: PipelineStartEPCallback | ||
| # EP: PipelineEarlySimplificationEPCallback | ||
| # EP: OptimizerEarlyEPCallback | ||
| # EP: OptimizerLastEPCallback | ||
|
|
||
| # Entry-points in optimizer pipeline | ||
| # | ||
| # RUN: env LLVM_PYPASS_SCRIPT=%s \ | ||
| # RUN: env LLVM_PYPASS_DYLIB=%libpython \ | ||
| # RUN: %clang -fpass-plugin=%plugindir/pypass-plugin%pluginext -O2 \ | ||
| # RUN: -o /dev/null -S -emit-llvm %S/Inputs/pypass-plugin.c | FileCheck --check-prefix=EP-OPT %s | ||
| # | ||
| # RUN: env LLVM_PYPASS_SCRIPT=%s \ | ||
| # RUN: env LLVM_PYPASS_DYLIB=%libpython \ | ||
| # RUN: %clang -fpass-plugin=%plugindir/pypass-plugin%pluginext -O2 -flto=full \ | ||
| # RUN: -o /dev/null -S -emit-llvm %S/Inputs/pypass-plugin.c | FileCheck --check-prefix=EP-OPT %s | ||
| # | ||
| # RUN: env LLVM_PYPASS_SCRIPT=%s \ | ||
| # RUN: env LLVM_PYPASS_DYLIB=%libpython \ | ||
| # RUN: %clang -fpass-plugin=%plugindir/pypass-plugin%pluginext -O2 -ffat-lto-objects \ | ||
| # RUN: -o /dev/null -S -emit-llvm %S/Inputs/pypass-plugin.c | FileCheck --check-prefix=EP-OPT %s | ||
| # | ||
| # EP-OPT: PipelineStartEPCallback | ||
| # EP-OPT: PipelineEarlySimplificationEPCallback | ||
| # EP-OPT: PeepholeEPCallback | ||
| # EP-OPT: ScalarOptimizerLateEPCallback | ||
| # EP-OPT: PeepholeEPCallback | ||
| # EP-OPT: OptimizerEarlyEPCallback | ||
| # EP-OPT: VectorizerStartEPCallback | ||
| # EP-OPT: VectorizerEndEPCallback | ||
| # EP-OPT: OptimizerLastEPCallback | ||
|
|
||
| # FIXME: Thin-LTO does not invoke vectorizer callbacks | ||
| # | ||
| # RUN: env LLVM_PYPASS_SCRIPT=%s \ | ||
| # RUN: env LLVM_PYPASS_DYLIB=%libpython \ | ||
| # RUN: %clang -fpass-plugin=%plugindir/pypass-plugin%pluginext -O2 -flto=thin \ | ||
| # RUN: -o /dev/null -S -emit-llvm %S/Inputs/pypass-plugin.c | FileCheck --check-prefix=EP-LTO-THIN %s | ||
| # | ||
| # EP-LTO-THIN: PipelineStartEPCallback | ||
| # EP-LTO-THIN: PipelineEarlySimplificationEPCallback | ||
| # EP-LTO-THIN: PeepholeEPCallback | ||
| # EP-LTO-THIN: ScalarOptimizerLateEPCallback | ||
| # EP-LTO-THIN: OptimizerEarlyEPCallback | ||
| # EP-LTO-THIN-NOT: Vectorizer{{.*}}EPCallback | ||
| # EP-LTO-THIN: OptimizerLastEPCallback | ||
|
|
||
|
|
||
| def registerPipelineStartEPCallback(): | ||
| """Module pass at the start of the pipeline""" | ||
| return True | ||
|
|
||
|
|
||
| def registerPipelineEarlySimplificationEPCallback(): | ||
| """Module pass after basic simplification of input IR""" | ||
| return True | ||
|
|
||
|
|
||
| def registerOptimizerEarlyEPCallback(): | ||
| """Module pass before the function optimization pipeline""" | ||
| return True | ||
|
|
||
|
|
||
| def registerOptimizerLastEPCallback(): | ||
| """Module pass after the function optimization pipeline""" | ||
| return True | ||
|
|
||
|
|
||
| def registerPeepholeEPCallback(): | ||
| """Function pass after each instance of the instruction combiner pass""" | ||
| return True | ||
|
|
||
|
|
||
| def registerScalarOptimizerLateEPCallback(): | ||
| """Function pass after most of the main optimizations, but before the last | ||
| cleanup-ish optimizations""" | ||
| return True | ||
|
|
||
|
|
||
| def registerVectorizerStartEPCallback(): | ||
| """Function pass before the vectorizer and other highly target specific | ||
| optimization passes are executed""" | ||
| return True | ||
|
|
||
|
|
||
| def registerVectorizerEndEPCallback(): | ||
| """Function pass after the vectorizer and other highly target specific | ||
| optimization passes are executed""" | ||
| return True | ||
|
|
||
|
|
||
| def registerFullLinkTimeOptimizationEarlyEPCallback(): | ||
| """Module pass at the start of the full LTO pipeline""" | ||
| return True | ||
|
|
||
|
|
||
| def registerFullLinkTimeOptimizationLastEPCallback(): | ||
| """Module pass at the end of the full LTO pipeline""" | ||
| return True | ||
|
|
||
|
|
||
| def run(input, ctx, stage): | ||
| print(f"0x{input:016x} {stage}") | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # REQUIRES: native, system-linux, llvm-dylib, plugins, pypass-plugin | ||
|
|
||
| # XFAIL: * | ||
| # | ||
| # RUN: %clang -fpass-plugin=%plugindir/pypass-plugin%pluginext -S -emit-llvm \ | ||
| # RUN: -mllvm -pypass-script=%s \ | ||
| # RUN: -mllvm -pypass-dylib=%libpython \ | ||
| # RUN: -Xclang -fdebug-pass-manager \ | ||
| # RUN: -o /dev/null -S -emit-llvm %S/Inputs/pypass-plugin.c 2>&1 | FileCheck %s | ||
| # | ||
| # CHECK: Unknown command line argument | ||
|
|
||
| # Plugin parameters only work with the extra `-Xclang -load -Xclang <path>` | ||
| # RUN: %clang -fpass-plugin=%plugindir/pypass-plugin%pluginext -S -emit-llvm \ | ||
| # RUN: -Xclang -load -Xclang %plugindir/pypass-plugin%pluginext \ | ||
| # RUN: -mllvm -pypass-script=%s \ | ||
| # RUN: -mllvm -pypass-dylib=%libpython \ | ||
| # RUN: -Xclang -fdebug-pass-manager \ | ||
| # RUN: -o /dev/null -S -emit-llvm %S/Inputs/pypass-plugin.c 2>&1 | FileCheck %s | ||
| # | ||
| # CHECK: Running pass: PyPass | ||
|
|
||
|
|
||
| def registerPipelineEarlySimplificationEPCallback(): | ||
| """Module pass after basic simplification of input IR""" | ||
| return True | ||
|
|
||
|
|
||
| def run(input, ctx, stage): | ||
| print(f"0x{input:016x} {stage}") |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # REQUIRES: native, system-linux, llvm-dylib, plugins, pypass-plugin | ||
|
|
||
| # RUN: echo "int a = 1;" | \ | ||
| # RUN: env LLVM_PYPASS_SCRIPT=%s \ | ||
| # RUN: env LLVM_PYPASS_DYLIB=%libpython \ | ||
| # RUN: clang-repl -Xcc -fpass-plugin=%plugindir/pypass-plugin%pluginext | FileCheck %s | ||
| # | ||
| # CHECK: PipelineEarlySimplificationEPCallback | ||
|
|
||
|
|
||
| def registerPipelineEarlySimplificationEPCallback(): | ||
| """Module pass after basic simplification of input IR""" | ||
| return True | ||
|
|
||
|
|
||
| def run(input, ctx, stage): | ||
| print(f"0x{input:016x} {stage}") |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" | ||
| target triple = "x86_64-unknown-linux-gnu" | ||
|
|
||
| define i32 @main() { | ||
| entry: | ||
| ret i32 0 | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import os | ||
| import platform | ||
| import sysconfig | ||
|
|
||
| pypass_plugin = f"pypass-plugin{config.llvm_shlib_ext}" | ||
| pypass_plugin_shlib = os.path.join(config.llvm_shlib_dir, pypass_plugin) | ||
|
|
||
| # Run end-to-end tests if the reference pass-plugin exists in LLVM | ||
| if os.path.exists(pypass_plugin_shlib): | ||
| config.available_features.add("pypass-plugin") | ||
|
|
||
| # Disable ASAN's leak detection for Python tests | ||
| config.environment["ASAN_OPTIONS"] = "detect_leaks=0" | ||
|
|
||
| if platform.system() != "Windows": | ||
| libdir = sysconfig.get_config_var("LIBDIR") | ||
| dylib = sysconfig.get_config_var("LDLIBRARY") | ||
| config.substitutions.append(("%libpython", os.path.join(libdir, dylib))) | ||
|
|
||
| # FIXME: %llvmshlibdir is broken in standalone builds | ||
| config.substitutions.append(("%plugindir", config.llvm_libs_dir)) | ||
| config.substitutions.append(("%pluginext", config.llvm_shlib_ext)) | ||
| config.suffixes.add(".py") |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| # REQUIRES: native, system-linux, llvm-dylib, plugins, pypass-plugin | ||
|
|
||
| # Entry-points in pipeline for regular/monolithic LTO | ||
| # | ||
| # RUN: opt %S/Inputs/pypass-plugin.ll -o %t.o | ||
| # | ||
| # RUN: env LLVM_PYPASS_SCRIPT=%s \ | ||
| # RUN: env LLVM_PYPASS_DYLIB=%libpython \ | ||
| # RUN: ld.lld --load-pass-plugin=%plugindir/pypass-plugin%pluginext %t.o \ | ||
| # RUN: -shared -o /dev/null | FileCheck --check-prefix=REGULAR %s | ||
| # | ||
| # REGULAR-NOT: PipelineStartEPCallback | ||
| # REGULAR-NOT: PipelineEarlySimplificationEPCallback | ||
| # REGULAR-NOT: PeepholeEPCallback | ||
| # REGULAR-NOT: ScalarOptimizerLateEPCallback | ||
| # REGULAR-NOT: Vectorizer{{.*}}EPCallback | ||
| # REGULAR-NOT: Optimizer{{.*}}EPCallback | ||
| # | ||
| # REGULAR: FullLinkTimeOptimizationEarlyEPCallback | ||
| # REGULAR: FullLinkTimeOptimizationLastEPCallback | ||
|
|
||
| # Entry-points in Thin-LTO pipeline | ||
| # | ||
| # RUN: opt --thinlto-bc %S/Inputs/pypass-plugin.ll -o %t_thin1.o | ||
| # RUN: opt -module-summary %S/Inputs/pypass-plugin.ll -o %t_thin2.bc | ||
| # | ||
| # RUN: env LLVM_PYPASS_SCRIPT=%s \ | ||
| # RUN: env LLVM_PYPASS_DYLIB=%libpython \ | ||
| # RUN: ld.lld --load-pass-plugin=%plugindir/pypass-plugin%pluginext %t_thin2.bc \ | ||
| # RUN: -shared -o /dev/null | FileCheck --check-prefix=THIN %s | ||
| # | ||
| # THIN-NOT: FullLinkTimeOptimizationEarlyEPCallback | ||
| # THIN-NOT: FullLinkTimeOptimizationLastEPCallback | ||
| # THIN-NOT: PipelineStartEPCallback | ||
| # | ||
| # THIN: PipelineEarlySimplificationEPCallback | ||
| # THIN: PeepholeEPCallback | ||
| # THIN: ScalarOptimizerLateEPCallback | ||
| # THIN: PeepholeEPCallback | ||
| # THIN: OptimizerEarlyEPCallback | ||
| # THIN: VectorizerStartEPCallback | ||
| # THIN: VectorizerEndEPCallback | ||
| # THIN: OptimizerLastEPCallback | ||
|
|
||
|
|
||
| def registerPipelineStartEPCallback(): | ||
| """Module pass at the start of the pipeline""" | ||
| return True | ||
|
|
||
|
|
||
| def registerPipelineEarlySimplificationEPCallback(): | ||
| """Module pass after basic simplification of input IR""" | ||
| return True | ||
|
|
||
|
|
||
| def registerOptimizerEarlyEPCallback(): | ||
| """Module pass before the function optimization pipeline""" | ||
| return True | ||
|
|
||
|
|
||
| def registerOptimizerLastEPCallback(): | ||
| """Module pass after the function optimization pipeline""" | ||
| return True | ||
|
|
||
|
|
||
| def registerPeepholeEPCallback(): | ||
| """Function pass after each instance of the instruction combiner pass""" | ||
| return True | ||
|
|
||
|
|
||
| def registerScalarOptimizerLateEPCallback(): | ||
| """Function pass after most of the main optimizations, but before the last | ||
| cleanup-ish optimizations""" | ||
| return True | ||
|
|
||
|
|
||
| def registerVectorizerStartEPCallback(): | ||
| """Function pass before the vectorizer and other highly target specific | ||
| optimization passes are executed""" | ||
| return True | ||
|
|
||
|
|
||
| def registerVectorizerEndEPCallback(): | ||
| """Function pass after the vectorizer and other highly target specific | ||
| optimization passes are executed""" | ||
| return True | ||
|
|
||
|
|
||
| def registerFullLinkTimeOptimizationEarlyEPCallback(): | ||
| """Module pass at the start of the full LTO pipeline""" | ||
| return True | ||
|
|
||
|
|
||
| def registerFullLinkTimeOptimizationLastEPCallback(): | ||
| """Module pass at the end of the full LTO pipeline""" | ||
| return True | ||
|
|
||
|
|
||
| def run(input, ctx, stage): | ||
| print(f"0x{input:016x} {stage}") |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # REQUIRES: native, system-linux, llvm-dylib, plugins, pypass-plugin | ||
| # | ||
| # RUN: opt %S/Inputs/pypass-plugin.ll -o %t.o | ||
| # | ||
| # RUN: env LLVM_PYPASS_SCRIPT=%s \ | ||
| # RUN: env LLVM_PYPASS_DYLIB=%libpython \ | ||
| # RUN: ld.lld --load-pass-plugin=%plugindir/pypass-plugin%pluginext \ | ||
| # RUN: --lto-newpm-passes=pypass %t.o -o /dev/null | FileCheck %s | ||
| # | ||
| # CHECK: 0x{{[0-9a-f]+}} Module | ||
|
|
||
|
|
||
| def registerModulePipelineParsingCallback(): | ||
| return True | ||
|
|
||
|
|
||
| def run(input, ctx, stage): | ||
| print(f"0x{input:016x} {stage}") |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.