Skip to content

Commit 9a79182

Browse files
wenju-hesys-ce-bb
authored andcommitted
Add pass plugin for in-tree build with shared libs (#2290)
The plugin registers llvm-spirv passes to llvm pass manager and enables LIT test for llvm-spirv passes using opt tool. Original commit: KhronosGroup/SPIRV-LLVM-Translator@346b4cd
1 parent 6e97ea4 commit 9a79182

File tree

6 files changed

+166
-0
lines changed

6 files changed

+166
-0
lines changed

llvm-spirv/lib/SPIRV/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ set(SRC_LIST
2626
SPIRVUtil.cpp
2727
SPIRVWriter.cpp
2828
SPIRVWriterPass.cpp
29+
PassPlugin.cpp
2930
PreprocessMetadata.cpp
3031
libSPIRV/SPIRVBasicBlock.cpp
3132
libSPIRV/SPIRVDebug.cpp

llvm-spirv/lib/SPIRV/PassPlugin.cpp

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
//===- PassPlugin.cpp - Register SPIRV passes as plugin -------------------===//
2+
//
3+
// The LLVM/SPIR-V Translator
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
// Copyright (c) 2024 The Khronos Group Inc.
9+
//
10+
// Permission is hereby granted, free of charge, to any person obtaining a
11+
// copy of this software and associated documentation files (the "Software"),
12+
// to deal with the Software without restriction, including without limitation
13+
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
14+
// and/or sell copies of the Software, and to permit persons to whom the
15+
// Software is furnished to do so, subject to the following conditions:
16+
//
17+
// Redistributions of source code must retain the above copyright notice,
18+
// this list of conditions and the following disclaimers.
19+
// Redistributions in binary form must reproduce the above copyright notice,
20+
// this list of conditions and the following disclaimers in the documentation
21+
// and/or other materials provided with the distribution.
22+
// Neither the names of The Khronos Group, nor the names of its
23+
// contributors may be used to endorse or promote products derived from this
24+
// Software without specific prior written permission.
25+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28+
// CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
31+
// THE SOFTWARE.
32+
//
33+
//===----------------------------------------------------------------------===//
34+
//
35+
// This file implements pass plugin to register llvm-spirv passes for opt tool.
36+
//
37+
//===----------------------------------------------------------------------===//
38+
39+
#include "OCLToSPIRV.h"
40+
#include "PreprocessMetadata.h"
41+
#include "SPIRVLowerBitCastToNonStandardType.h"
42+
#include "SPIRVLowerBool.h"
43+
#include "SPIRVLowerConstExpr.h"
44+
#include "SPIRVLowerMemmove.h"
45+
#include "SPIRVLowerOCLBlocks.h"
46+
#include "SPIRVLowerSaddWithOverflow.h"
47+
#include "SPIRVRegularizeLLVM.h"
48+
#include "SPIRVToOCL.h"
49+
#include "SPIRVWriter.h"
50+
51+
#include "llvm/Passes/PassBuilder.h"
52+
#include "llvm/Passes/PassPlugin.h"
53+
54+
using namespace llvm;
55+
56+
namespace {
57+
58+
PassPluginLibraryInfo getSPIRVPluginInfo() {
59+
return {
60+
LLVM_PLUGIN_API_VERSION, "SPIRV", LLVM_VERSION_STRING,
61+
[](PassBuilder &PB) {
62+
PB.registerAnalysisRegistrationCallback([](ModuleAnalysisManager &AM) {
63+
AM.registerPass([] { return OCLTypeToSPIRVPass(); });
64+
});
65+
PB.registerPipelineParsingCallback(
66+
[](StringRef Name, FunctionPassManager &PM,
67+
ArrayRef<PassBuilder::PipelineElement>) {
68+
if (Name.equals("spirv-lower-bitcast")) {
69+
PM.addPass(
70+
SPIRVLowerBitCastToNonStandardTypePass(TranslatorOpts{}));
71+
return true;
72+
}
73+
return false;
74+
});
75+
PB.registerPipelineParsingCallback(
76+
[](StringRef Name, ModulePassManager &PM,
77+
ArrayRef<PassBuilder::PipelineElement>) {
78+
if (Name.equals("ocl-to-spirv")) {
79+
PM.addPass(OCLToSPIRVPass());
80+
return true;
81+
}
82+
if (Name.equals("llvm-to-spirv")) {
83+
SPIRV::TranslatorOpts DefaultOpts;
84+
DefaultOpts.enableAllExtensions();
85+
SPIRVModule *BM = SPIRVModule::createSPIRVModule(DefaultOpts);
86+
PM.addPass(LLVMToSPIRVPass(BM));
87+
return true;
88+
}
89+
if (Name.equals("process-metadata")) {
90+
PM.addPass(PreprocessMetadataPass());
91+
return true;
92+
}
93+
if (Name.equals("spirv-lower-bool")) {
94+
PM.addPass(SPIRVLowerBoolPass());
95+
return true;
96+
}
97+
if (Name.equals("spirv-lower-constexpr")) {
98+
PM.addPass(SPIRVLowerConstExprPass());
99+
return true;
100+
}
101+
if (Name.equals("spirv-lower-memmove")) {
102+
PM.addPass(SPIRVLowerMemmovePass());
103+
return true;
104+
}
105+
if (Name.equals("spirv-lower-ocl-blocks")) {
106+
PM.addPass(SPIRVLowerOCLBlocksPass());
107+
return true;
108+
}
109+
if (Name.equals("spirv-lower-sadd-with-overflow")) {
110+
PM.addPass(SPIRVLowerSaddWithOverflowPass());
111+
return true;
112+
}
113+
if (Name.equals("spirv-regularize-llvm")) {
114+
PM.addPass(SPIRVRegularizeLLVMPass());
115+
return true;
116+
}
117+
if (Name.equals("spirv-to-ocl12")) {
118+
PM.addPass(SPIRVToOCL12Pass());
119+
return true;
120+
}
121+
if (Name.equals("spirv-to-ocl20")) {
122+
PM.addPass(SPIRVToOCL20Pass());
123+
return true;
124+
}
125+
return false;
126+
});
127+
}};
128+
}
129+
130+
} // namespace
131+
132+
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo
133+
llvmGetPassPluginInfo() {
134+
return getSPIRVPluginInfo();
135+
}

llvm-spirv/test/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
llvm_canonicalize_cmake_booleans(SPIRV_SKIP_CLANG_BUILD)
22
llvm_canonicalize_cmake_booleans(SPIRV_SKIP_DEBUG_INFO_TESTS)
3+
llvm_canonicalize_cmake_booleans(LLVM_BUILD_SHARED_LIBS)
4+
llvm_canonicalize_cmake_booleans(LLVM_SPIRV_BUILD_EXTERNAL)
35

46
# required by lit.site.cfg.py.in
57
get_target_property(LLVM_SPIRV_DIR llvm-spirv BINARY_DIR)
@@ -96,6 +98,11 @@ if(NOT LLVM_SPIRV_BUILD_EXTERNAL)
9698
llvm-readobj
9799
)
98100
endif(NOT SPIRV_SKIP_DEBUG_INFO_TESTS)
101+
if(LLVM_BUILD_SHARED_LIBS)
102+
list(APPEND LLVM_SPIRV_TEST_DEPS
103+
opt
104+
)
105+
endif()
99106
endif(NOT LLVM_SPIRV_BUILD_EXTERNAL)
100107

101108

llvm-spirv/test/lit.cfg.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@
7676
else:
7777
config.substitutions.append(('spirv-val', ':'))
7878

79+
if not config.llvm_spirv_build_external and config.llvm_build_shared_libs:
80+
config.available_features.add('pass-plugin')
81+
config.substitutions.append(
82+
(
83+
"%load_spirv_lib",
84+
"-load-pass-plugin={}/libLLVMSPIRVLib{}".format(
85+
config.llvm_shlib_dir, config.llvm_plugin_ext
86+
),
87+
)
88+
)
89+
7990
llvm_config.with_system_environment('LD_LIBRARY_PATH')
8091
if using_spirv_tools:
8192
llvm_config.with_environment('LD_LIBRARY_PATH', config.spirv_tools_lib_dir, append_path=True)

llvm-spirv/test/lit.site.cfg.py.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ config.llvm_spirv_lib_dir = "@LLVM_SPIRV_LIB_DIR@"
1010
config.llvm_libs_dir = "@LLVM_LIBS_DIR@"
1111
config.llvm_shlib_dir = "@SHLIBDIR@"
1212
config.llvm_plugin_ext = "@LLVM_PLUGIN_EXT@"
13+
config.llvm_build_shared_libs = @LLVM_BUILD_SHARED_LIBS@
14+
config.llvm_spirv_build_external = @LLVM_SPIRV_BUILD_EXTERNAL@
1315
config.lit_tools_dir = "@LLVM_LIT_TOOLS_DIR@"
1416
config.host_triple = "@LLVM_HOST_TRIPLE@"
1517
config.target_triple = "@LLVM_TARGET_TRIPLE@"

llvm-spirv/test/pass-plugin-opt.ll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
; REQUIRES: pass-plugin
2+
; UNSUPPORTED: target={{.*windows.*}}
3+
4+
; RUN: opt %load_spirv_lib -passes=llvm-to-spirv -disable-output -debug-pass-manager %s 2>&1 | FileCheck %s
5+
6+
; CHECK: Running pass: SPIRV::LLVMToSPIRVPass
7+
; CHECK: Running analysis: SPIRV::OCLTypeToSPIRVPass
8+
9+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
10+
target triple = "spir64-unknown-unknown"

0 commit comments

Comments
 (0)