Skip to content

[Instrumentor] A configurable instrumentation pass plugin #151551

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions clang/test/Driver/pass-plugin-entrypoints.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// REQUIRES: pass-plugins
// UNSUPPORTED: system-windows

// Default entry-point is Pipeline-EarlySimplification
//
// RUN: %clang -O0 -fpass-plugin=%pass_plugin_reference \
// RUN: -S -emit-llvm -Xclang -fdebug-pass-manager %s -o /dev/null 2>&1 | \
// RUN: FileCheck --check-prefix=EP-EARLY %s
//
// RUN: %clang -O2 -fpass-plugin=%pass_plugin_reference \
// RUN: -S -emit-llvm -Xclang -fdebug-pass-manager %s -o /dev/null 2>&1 | \
// RUN: FileCheck --check-prefix=EP-EARLY %s
//
// RUN: %clang -c -flto=full -O2 -fpass-plugin=%pass_plugin_reference \
// RUN: -Xclang -fdebug-pass-manager %s -o /dev/null 2>&1 | \
// RUN: FileCheck --check-prefix=EP-EARLY %s
//
// RUN: %clang -c -flto=thin -O2 -fpass-plugin=%pass_plugin_reference \
// RUN: -Xclang -fdebug-pass-manager %s -o /dev/null 2>&1 | \
// RUN: FileCheck --check-prefix=EP-EARLY %s
//
// EP-EARLY: Running pass: InstrumentorPass
// EP-EARLY: Running pass: AlwaysInlinerPass

// Pass doesn't run if default entry-point is disabled
// RUN: env registerPipelineEarlySimplificationEPCallback=Off \
// RUN: %clang -fpass-plugin=%pass_plugin_reference -S -emit-llvm \
// RUN: -Xclang -fdebug-pass-manager %s -o /dev/null 2>&1 | FileCheck %s
//
// CHECK-NOT: Running pass: InstrumentorPass

// Pass runs twice if we add entry-point Opt-Early
// RUN: env registerOptimizerEarlyEPCallback=On \
// RUN: %clang -fpass-plugin=%pass_plugin_reference -S -emit-llvm \
// RUN: -Xclang -fdebug-pass-manager %s -o /dev/null 2>&1 | FileCheck --check-prefix=OPT-EARLY %s
//
// OPT-EARLY: Running pass: InstrumentorPass
// OPT-EARLY: Running pass: AlwaysInlinerPass
// OPT-EARLY: Running pass: InstrumentorPass

int main() { return 0; }
13 changes: 13 additions & 0 deletions clang/test/Driver/pass-plugin-params.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// REQUIRES: pass-plugins
// UNSUPPORTED: system-windows

// FIXME: This is supposed to work, but right now it doesn't. We need the extra -load like below.
// RUN: not %clang -fsyntax-only -fpass-plugin=%pass_plugin_reference \
// RUN: -mllvm -instrumentor-write-config-file=%t_cfg.json %s 2>&1 | FileCheck %s
//
// RUN: %clang -fsyntax-only -fpass-plugin=%pass_plugin_reference -Xclang -load -Xclang %pass_plugin_reference \
// RUN: -mllvm -instrumentor-write-config-file=%t_cfg.json %s
//
// CHECK: Unknown command line argument

int main() { return 0; }
7 changes: 7 additions & 0 deletions clang/test/lit.cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ def have_host_clang_repl_cuda():
if config.clang_default_pie_on_linux:
config.available_features.add("default-pie-on-linux")

# Run end-to-end tests if the reference pass-plugin exists in LLVM
plugin_name = f"{config.llvm_plugin_prefix}ReferencePlugin{config.llvm_plugin_ext}"
plugin_shlib = os.path.join(config.llvm_shlib_dir, plugin_name)
if os.path.exists(plugin_shlib):
config.available_features.add("pass-plugins")
config.substitutions.append(("%pass_plugin_reference", plugin_shlib))

# Set available features we allow tests to conditionalize on.
#
if config.clang_default_cxx_stdlib != "":
Expand Down
1 change: 1 addition & 0 deletions clang/test/lit.site.cfg.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ config.have_llvm_driver = @LLVM_TOOL_LLVM_DRIVER_BUILD@
config.spirv_tools_tests = @LLVM_INCLUDE_SPIRV_TOOLS_TESTS@
config.substitutions.append(("%llvm-version-major", "@LLVM_VERSION_MAJOR@"))
config.has_key_instructions = @LLVM_EXPERIMENTAL_KEY_INSTRUCTIONS@
config.llvm_plugin_prefix = "@CMAKE_SHARED_LIBRARY_PREFIX@"

import lit.llvm
lit.llvm.initialize(lit_config, config)
Expand Down
7 changes: 7 additions & 0 deletions llvm/test/lit.cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,3 +700,10 @@ def host_unwind_supports_jit():

if config.has_logf128:
config.available_features.add("has_logf128")

# Run tests with opt if the reference pass-plugin exists
plugin_name = f"{config.llvm_plugin_prefix}ReferencePlugin{config.llvm_plugin_ext}"
plugin_shlib = os.path.join(config.llvm_shlib_dir, plugin_name)
if os.path.exists(plugin_shlib):
config.available_features.add("pass-plugins")
config.substitutions.append(("%pass_plugin_reference", plugin_shlib))
1 change: 1 addition & 0 deletions llvm/test/lit.site.cfg.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ config.llvm_lib_dir = lit_config.substitute(path(r"@LLVM_LIBS_DIR@"))
config.llvm_shlib_dir = lit_config.substitute(path(r"@SHLIBDIR@"))
config.llvm_shlib_ext = "@SHLIBEXT@"
config.llvm_plugin_ext = "@LLVM_PLUGIN_EXT@"
config.llvm_plugin_prefix = "@CMAKE_SHARED_LIBRARY_PREFIX@"
config.llvm_exe_ext = "@EXEEXT@"
config.lit_tools_dir = path(r"@LLVM_LIT_TOOLS_DIR@")
config.errc_messages = "@LLVM_LIT_ERRC_MESSAGES@"
Expand Down
63 changes: 63 additions & 0 deletions llvm/test/tools/plugins-shlib/alloca.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt < %s -load-pass-plugin=%pass_plugin_reference -passes=instrumentor -S | FileCheck %s
; REQUIRES: pass-plugins

target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

define ptr @foo() {
; CHECK-LABEL: define ptr @foo() {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[A:%.*]] = alloca ptr, align 8
; CHECK-NEXT: [[TMP0:%.*]] = call ptr @__instrumentor_alloca(ptr [[A]], i64 8, i64 8)
; CHECK-NEXT: ret ptr [[TMP0]]
;
entry:
%a = alloca ptr
ret ptr %a
}
define ptr @bar(i1 %c) {
; CHECK-LABEL: define ptr @bar(
; CHECK-SAME: i1 [[C:%.*]]) {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[B:%.*]] = alloca i32, i32 5, align 4
; CHECK-NEXT: [[TMP0:%.*]] = call ptr @__instrumentor_alloca(ptr [[B]], i64 20, i64 4)
; CHECK-NEXT: [[TMP1:%.*]] = call ptr @__instrumentor_alloca(ptr [[A]], i64 4, i64 4)
; CHECK-NEXT: [[S:%.*]] = select i1 [[C]], ptr [[TMP1]], ptr [[TMP0]]
; CHECK-NEXT: ret ptr [[S]]
;
entry:
%a = alloca i32
%b = alloca i32, i32 5
%s = select i1 %c, ptr %a, ptr %b
ret ptr %s
}
define ptr @baz(i32 %v) {
; CHECK-LABEL: define ptr @baz(
; CHECK-SAME: i32 [[V:%.*]]) {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[A:%.*]] = alloca ptr, i32 [[V]], align 8
; CHECK-NEXT: [[TMP0:%.*]] = zext i32 [[V]] to i64
; CHECK-NEXT: [[TMP2:%.*]] = mul i64 8, [[TMP0]]
; CHECK-NEXT: [[TMP1:%.*]] = call ptr @__instrumentor_alloca(ptr [[A]], i64 [[TMP2]], i64 8)
; CHECK-NEXT: ret ptr [[TMP1]]
;
entry:
%a = alloca ptr, i32 %v
ret ptr %a
}
define ptr @fizz() {
; CHECK-LABEL: define ptr @fizz() {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[A:%.*]] = alloca <vscale x 2 x i32>, align 8
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr <vscale x 2 x i32>, ptr [[A]], i32 1
; CHECK-NEXT: [[TMP2:%.*]] = ptrtoint ptr [[TMP1]] to i64
; CHECK-NEXT: [[TMP0:%.*]] = ptrtoint ptr [[A]] to i64
; CHECK-NEXT: [[TMP3:%.*]] = sub i64 [[TMP2]], [[TMP0]]
; CHECK-NEXT: [[TMP4:%.*]] = call ptr @__instrumentor_alloca(ptr [[A]], i64 [[TMP3]], i64 8)
; CHECK-NEXT: ret ptr [[TMP4]]
;
entry:
%a = alloca <vscale x 2 x i32>
ret ptr %a
}
13 changes: 13 additions & 0 deletions llvm/test/tools/plugins-shlib/default_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Base": {
"RuntimeName": "__instrumentor_",
"PrintRuntimeSignatures": true
},
"Alloca": {
"Instrument": true,
"Value": true,
"AllocationSize": true,
"Alignment": true,
"ReplaceValue": true
}
}
14 changes: 14 additions & 0 deletions llvm/test/tools/plugins-shlib/just_calls_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Base": {
"RuntimeName": "__just_calls_",
"PrintRuntimeSignatures": false
},
"Alloca": {
"Instrument": true,
"Value": false,
"AllocationSize": false,
"Alignment": false,
"ArraySize": false,
"ReplaceValue": false
}
}
22 changes: 22 additions & 0 deletions llvm/test/tools/plugins-shlib/read_config.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt < %s -load-pass-plugin=%pass_plugin_reference -passes=instrumentor -instrumentor-read-config-file=%S/just_calls_config.json -S | FileCheck %s

target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

define ptr @bar(i1 %c) {
; CHECK-LABEL: define ptr @bar(
; CHECK-SAME: i1 [[C:%.*]]) {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[B:%.*]] = alloca i32, i32 5, align 4
; CHECK-NEXT: call void @__just_calls_alloca()
; CHECK-NEXT: call void @__just_calls_alloca()
; CHECK-NEXT: [[S:%.*]] = select i1 [[C]], ptr [[A]], ptr [[B]]
; CHECK-NEXT: ret ptr [[S]]
;
entry:
%a = alloca i32
%b = alloca i32, i32 5
%s = select i1 %c, ptr %a, ptr %b
ret ptr %s
}
3 changes: 3 additions & 0 deletions llvm/test/tools/plugins-shlib/write_config.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt < %s -load-pass-plugin=%pass_plugin_reference -passes=instrumentor -instrumentor-write-config-file=%t.json
; RUN: diff %t.json %S/default_config.json
7 changes: 7 additions & 0 deletions llvm/tools/plugins-shlib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
add_llvm_library(ReferencePlugin SHARED INSTALL_WITH_TOOLCHAIN
Instrumentor.cpp
ReferencePlugin.cpp

DEPENDS
intrinsics_gen
)
Loading
Loading