Skip to content

Commit 2f8705d

Browse files
author
serge-sans-paille
committed
Compiler extension patch
1 parent 760094e commit 2f8705d

File tree

8 files changed

+54
-40
lines changed

8 files changed

+54
-40
lines changed

llvm/cmake/modules/AddLLVM.cmake

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -847,32 +847,40 @@ macro(add_llvm_executable name)
847847
llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH})
848848
endmacro(add_llvm_executable name)
849849

850-
# add_llvm_pass_plugin(name)
850+
# add_llvm_pass_plugin(name [NO_MODULE] ...)
851851
# Add ${name} as an llvm plugin.
852852
# If option LLVM_${name_upper}_LINK_INTO_TOOLS is set to ON, the plugin is registered statically.
853853
# Otherwise a pluggable shared library is registered.
854+
#
855+
# If NO_MODULE is specified, when option LLVM_${name_upper}_LINK_INTO_TOOLS is set to OFF,
856+
# only an object library is built, and no module is built. This is specific to the Polly use case.
854857
function(add_llvm_pass_plugin name)
858+
cmake_parse_arguments(ARG
859+
"NO_MODULE" "" ""
860+
${ARGN})
855861

856862
string(TOUPPER ${name} name_upper)
857863

858864
option(LLVM_${name_upper}_LINK_INTO_TOOLS "Statically link ${name} into tools (if available)" OFF)
859865

860-
# process_llvm_pass_plugins takes care of the actual linking, just create an
861-
# object library as of now
862-
add_llvm_library(${name} OBJECT ${ARGN})
863-
864-
if(LLVM_${name_upper}_LINK_INTO_TOOLS)
865-
target_compile_definitions(${name} PRIVATE LLVM_${name_upper}_LINK_INTO_TOOLS)
866-
set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS LLVM_LINK_INTO_TOOLS)
867-
if (TARGET intrinsics_gen)
868-
add_dependencies(obj.${name} intrinsics_gen)
869-
endif()
870-
endif()
871-
872-
message(STATUS "Registering ${name} as a pass plugin (static build: ${LLVM_${name_upper}_LINK_INTO_TOOLS})")
873866
if(LLVM_${name_upper}_LINK_INTO_TOOLS)
867+
list(REMOVE_ITEM ARG_UNPARSED_ARGUMENTS BUILDTREE_ONLY)
868+
# process_llvm_pass_plugins takes care of the actual linking, just create an
869+
# object library as of now
870+
add_llvm_library(${name} OBJECT ${ARG_UNPARSED_ARGUMENTS})
871+
target_compile_definitions(${name} PRIVATE LLVM_${name_upper}_LINK_INTO_TOOLS)
872+
set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS LLVM_LINK_INTO_TOOLS)
873+
if (TARGET intrinsics_gen)
874+
add_dependencies(obj.${name} intrinsics_gen)
875+
endif()
876+
message(STATUS "Registering ${name} as a pass plugin (static build: ${LLVM_${name_upper}_LINK_INTO_TOOLS})")
874877
set_property(GLOBAL APPEND PROPERTY LLVM_COMPILE_EXTENSIONS ${name})
878+
elseif(NOT ARG_NO_MODULE)
879+
add_llvm_library(${name} MODULE ${ARG_UNPARSED_ARGUMENTS})
880+
else()
881+
add_llvm_library(${name} OBJECT ${ARG_UNPARSED_ARGUMENTS})
875882
endif()
883+
876884
endfunction(add_llvm_pass_plugin)
877885

878886
# Generate X Macro file for extension handling. It provides a

llvm/examples/Bye/CMakeLists.txt

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
1-
add_llvm_pass_plugin(Bye
2-
Bye.cpp
3-
DEPENDS
4-
intrinsics_gen
5-
BUILDTREE_ONLY
6-
)
7-
8-
if (LLVM_LINK_LLVM_DYLIB)
9-
target_link_libraries(Bye PUBLIC LLVM)
10-
else()
11-
target_link_libraries(Bye
12-
PUBLIC
13-
LLVMSupport
14-
LLVMCore
15-
LLVMipo
16-
LLVMPasses
17-
)
1+
if(LLVM_BYE_LINK_INTO_TOOLS)
2+
message(WARNING "Setting LLVM_BYE_LINK_INTO_TOOLS=ON only makes sense for testing purpose")
183
endif()
194

20-
if( LLVM_BUILD_EXAMPLES )
21-
install(TARGETS ${name} RUNTIME DESTINATION examples)
22-
endif()
5+
add_llvm_pass_plugin(Bye
6+
Bye.cpp
7+
DEPENDS
8+
intrinsics_gen
9+
BUILDTREE_ONLY
10+
)
11+
12+
install(TARGETS ${name} RUNTIME DESTINATION examples)
2313
set_target_properties(${name} PROPERTIES FOLDER "Examples")

llvm/include/llvm/Support/DynamicLibrary.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ namespace sys {
123123
/// Add searchable symbol/value pair.
124124
static void AddSymbol(StringRef symbolName, void *symbolValue);
125125

126+
/// Ensure the underlying library registry is properly constructed.
127+
/// This is only useful to force ManagedStatic registration ordering.
128+
static void ensureConstructed();
129+
126130
class HandleSet;
127131
};
128132

llvm/lib/Support/DynamicLibrary.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ void DynamicLibrary::AddSymbol(StringRef SymbolName, void *SymbolValue) {
145145
(*ExplicitSymbols)[SymbolName] = SymbolValue;
146146
}
147147

148+
void DynamicLibrary::ensureConstructed() {
149+
(void)*OpenedHandles;
150+
}
151+
152+
148153
DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *FileName,
149154
std::string *Err) {
150155
// Force OpenedHandles to be added into the ManagedStatic list before any

llvm/lib/Transforms/IPO/PassManagerBuilder.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/IR/LegacyPassManager.h"
2828
#include "llvm/IR/Verifier.h"
2929
#include "llvm/Support/CommandLine.h"
30+
#include "llvm/Support/DynamicLibrary.h"
3031
#include "llvm/Support/ManagedStatic.h"
3132
#include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h"
3233
#include "llvm/Transforms/IPO.h"
@@ -194,12 +195,20 @@ static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
194195
/// Since GlobalExtensions is a managed static, calling 'empty()' will trigger
195196
/// the construction of the object.
196197
static bool GlobalExtensionsNotEmpty() {
198+
// Dynamic library static registry **must** be initialized before global
199+
// extension registry, because global extension may reference functions from
200+
// dynamically opened libraries. Failing to ensure that order (randomly)
201+
// triggers a segfault upon destruction, if the dynamically loaded library is
202+
// closed before the global extensions are destroyed.
203+
sys::DynamicLibrary::ensureConstructed();
197204
return GlobalExtensions.isConstructed() && !GlobalExtensions->empty();
198205
}
199206

200207
void PassManagerBuilder::addGlobalExtension(
201208
PassManagerBuilder::ExtensionPointTy Ty,
202209
PassManagerBuilder::ExtensionFn Fn) {
210+
// See comment in GlobalExtensionsNotEmpty
211+
sys::DynamicLibrary::ensureConstructed();
203212
GlobalExtensions->push_back(std::make_pair(Ty, std::move(Fn)));
204213
}
205214

llvm/test/Feature/load_extension.ll

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
; This is currently failing on multiple platforms. Disable while investigation occurs.
2-
; XFAIL: *
3-
41
; RUN: opt %s %loadbye -goodbye -wave-goodbye -disable-output 2>&1 | FileCheck %s
5-
; REQUIRES: plugins, examples
2+
; REQUIRES: plugins
63
; CHECK: Bye
74

85
@junk = global i32 0

llvm/test/lit.cfg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,13 @@ def get_asan_rtlib():
200200
if config.build_examples:
201201
config.available_features.add('examples')
202202

203-
if config.linked_bye_extension and config.build_examples:
203+
if config.linked_bye_extension:
204204
config.substitutions.append(('%llvmcheckext', 'CHECK-EXT'))
205205
config.substitutions.append(('%loadbye', ''))
206206
else:
207207
config.substitutions.append(('%llvmcheckext', 'CHECK-NOEXT'))
208208
config.substitutions.append(('%loadbye',
209-
'-load={}/libBye{}'.format(config.llvm_shlib_dir,
209+
'-load={}/Bye{}'.format(config.llvm_shlib_dir,
210210
config.llvm_shlib_ext)))
211211

212212
# Static libraries are not built if BUILD_SHARED_LIBS is ON.

polly/lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ endif ()
2424
# Use an object-library to add the same files to multiple libs without requiring
2525
# the sources them to be recompiled for each of them.
2626
add_llvm_pass_plugin(Polly
27+
NO_MODULE
2728
Analysis/DependenceInfo.cpp
2829
Analysis/PolyhedralInfo.cpp
2930
Analysis/ScopDetection.cpp

0 commit comments

Comments
 (0)