Skip to content

Commit 114aeba

Browse files
jchlandaAaronBallmanpremanandraosteffenlarsen
authored
[LIBCLC] Generate FunctionDecl to remangle substituted entries (#7220)
Retire the by hand mangling of substituted function in favor of constructing corresponding `FunctionDecls` and letting clang mangle it for us. This ensures that we never diverge from clang's mangling idiosyncrasies and bugfixes would likely be in the AST creation, not mangling itself. The tool has been rewritten as a `ClangTool` implementing `FrontendAction`. Additionally, we provide the option to perform a test run, in which no substitutions are made, while checking that remangled name does not diverge from the original mangled name. The tests are added to `check-libclc` target. The diffs are a bit ugly, it might be easier to inspect the file mode, basically the change lives entirely in `Remangler` class, and if you follow `Remangler::remangle` it will lead you through what it does. Fixes: #6505 Co-authored-by: Aaron Ballman <[email protected]> Co-authored-by: premanandrao <[email protected]> Co-authored-by: Steffen Larsen <[email protected]>
1 parent c3ac2e0 commit 114aeba

File tree

4 files changed

+775
-455
lines changed

4 files changed

+775
-455
lines changed

libclc/cmake/modules/AddLibclc.cmake

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ macro(add_libclc_builtin_set arch_suffix)
108108

109109
# Generate remangled variants if requested
110110
if( LIBCLC_GENERATE_REMANGLED_VARIANTS )
111+
set(dummy_in "${CMAKE_BINARY_DIR}/lib/clc/libclc_dummy_in.cc")
112+
add_custom_command( OUTPUT ${dummy_in}
113+
COMMAND ${CMAKE_COMMAND} -E touch ${dummy_in} )
111114
set(long_widths l32 l64)
112115
set(char_signedness signed unsigned)
113116
if( ${obj_suffix} STREQUAL "libspirv-nvptx64--nvidiacl.bc")
@@ -128,10 +131,11 @@ macro(add_libclc_builtin_set arch_suffix)
128131
-o "${builtins_remangle_path}"
129132
--long-width=${long_width}
130133
--char-signedness=${signedness}
131-
"$<TARGET_PROPERTY:prepare-${obj_suffix},TARGET_FILE>"
132-
DEPENDS "${builtins_obj_path}" "prepare-${obj_suffix}" libclc-remangler )
134+
--input-ir="$<TARGET_PROPERTY:prepare-${obj_suffix},TARGET_FILE>"
135+
${dummy_in}
136+
DEPENDS "${builtins_obj_path}" "prepare-${obj_suffix}" libclc-remangler ${dummy_in})
133137
add_custom_target( "remangled-${long_width}-${signedness}_char.${obj_suffix_mangled}" ALL
134-
DEPENDS "${builtins_remangle_path}" )
138+
DEPENDS "${builtins_remangle_path}" "${dummy_in}")
135139
set_target_properties("remangled-${long_width}-${signedness}_char.${obj_suffix_mangled}"
136140
PROPERTIES TARGET_FILE "${builtins_remangle_path}")
137141

libclc/test/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,27 @@ foreach( t ${LIBCLC_TARGET_TO_TEST} )
4343

4444
endforeach( d )
4545
endforeach( t )
46+
47+
if(LIBCLC_GENERATE_REMANGLED_VARIANTS)
48+
# Run remangler in test mode if generating remangled variants and make sure
49+
# it depends on check-libclc target.
50+
# Both `long_widths` and `char_signedness` are set in AddLibclc.cmake and can
51+
# be used here.
52+
foreach(long_width ${long_widths})
53+
foreach(signedness ${char_signedness})
54+
# In `-t` (TestRun) the remangler does not perform any substitutions, it
55+
# needs to make sure that the remangled name matches the original mangled
56+
# one.
57+
set (test_target_name "test-remangled-${long_width}-${signedness}_char")
58+
add_custom_target(${test_target_name}
59+
COMMAND libclc-remangler
60+
--long-width=${long_width}
61+
--char-signedness=${signedness}
62+
--input-ir="$<TARGET_PROPERTY:prepare-${obj_suffix},TARGET_FILE>"
63+
${dummy_in} -t -o -
64+
DEPENDS "${builtins_obj_path}" "prepare-${obj_suffix}" "${dummy_in}" libclc-remangler)
65+
66+
add_dependencies(check-libclc ${test_target_name})
67+
endforeach()
68+
endforeach()
69+
endif()

libclc/utils/libclc-remangler/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,11 @@ target_include_directories(libclc-remangler PRIVATE
1414
${CMAKE_SOURCE_DIR}/../clang/include
1515
${CMAKE_BINARY_DIR}/tools/clang/include)
1616

17-
clang_target_link_libraries(libclc-remangler PRIVATE clangBasic)
17+
clang_target_link_libraries(libclc-remangler
18+
PRIVATE
19+
clangAST
20+
clangBasic
21+
clangFrontend
22+
clangTooling
23+
LLVMOption
24+
)

0 commit comments

Comments
 (0)