Skip to content

Commit 28d9255

Browse files
authored
[libclc] Override generic symbol using llvm-link --override flag instead of using weak linkage (#156778)
Before this PR, weak linkage is applied to a few CLC generic functions to allow target specific implementation to override generic one. However, adding weak linkage has a side effect of preventing inter-procedural optimization, such as PostOrderFunctionAttrsPass, because weak function doesn't have exact definition (as determined by hasExactDefinition in the pass). This PR resolves the issue by adding --override flag for every non-generic bitcode file in llvm-link run. This approach eliminates the need for weak linkage while still allowing target-specific implementation to override generic one. llvm-diff shows imporoved attribute deduction for some functions in amdgcn--amdhsa.bc, e.g. %23 = tail call half @llvm.sqrt.f16(half %22) => %23 = tail call noundef half @llvm.sqrt.f16(half %22)
1 parent a04a9de commit 28d9255

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

libclc/clc/lib/generic/math/clc_ldexp.cl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
#include <clc/relational/clc_isnan.h>
1515
#include <clc/shared/clc_clamp.h>
1616

17-
#define _CLC_DEF_ldexp _CLC_DEF __attribute__((weak))
18-
19-
_CLC_DEF_ldexp _CLC_OVERLOAD float __clc_ldexp(float x, int n) {
17+
_CLC_DEF _CLC_OVERLOAD float __clc_ldexp(float x, int n) {
2018

2119
if (!__clc_fp32_subnormals_supported()) {
2220
// This treats subnormals as zeros
@@ -89,7 +87,7 @@ _CLC_DEF_ldexp _CLC_OVERLOAD float __clc_ldexp(float x, int n) {
8987

9088
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
9189

92-
_CLC_DEF_ldexp _CLC_OVERLOAD double __clc_ldexp(double x, int n) {
90+
_CLC_DEF _CLC_OVERLOAD double __clc_ldexp(double x, int n) {
9391
long l = __clc_as_ulong(x);
9492
int e = (l >> 52) & 0x7ff;
9593
long s = l & 0x8000000000000000;
@@ -124,14 +122,13 @@ _CLC_DEF_ldexp _CLC_OVERLOAD double __clc_ldexp(double x, int n) {
124122

125123
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
126124

127-
_CLC_OVERLOAD _CLC_DEF_ldexp half __clc_ldexp(half x, int n) {
125+
_CLC_OVERLOAD _CLC_DEF half __clc_ldexp(half x, int n) {
128126
return (half)__clc_ldexp((float)x, n);
129127
}
130128

131129
#endif
132130

133131
#define __CLC_FUNCTION __clc_ldexp
134-
#define __CLC_DEF_SPEC _CLC_DEF_ldexp
135132
#define __CLC_ARG2_TYPE int
136133
#define __CLC_BODY <clc/shared/binary_def_scalarize.inc>
137134
#include <clc/math/gentype.inc>

libclc/clc/lib/generic/math/clc_rsqrt.inc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
__attribute__((weak)) _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE
10-
__clc_rsqrt(__CLC_GENTYPE val) {
9+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_rsqrt(__CLC_GENTYPE val) {
1110
#pragma clang fp contract(fast)
1211
return __CLC_FP_LIT(1.0) / __builtin_elementwise_sqrt(val);
1312
}

libclc/clc/lib/generic/math/clc_sqrt.inc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
__attribute__((weak)) _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE
10-
__clc_sqrt(__CLC_GENTYPE val) {
9+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_sqrt(__CLC_GENTYPE val) {
1110
return __builtin_elementwise_sqrt(val);
1211
}

libclc/cmake/modules/AddLibclc.cmake

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,35 @@ function(link_bc)
9292
${ARGN}
9393
)
9494

95-
set( LINK_INPUT_ARG ${ARG_INPUTS} )
95+
if( ARG_INTERNALIZE )
96+
set( inputs_with_flag ${ARG_INPUTS} )
97+
else()
98+
# Add the --override flag for non-generic bitcode files so that their
99+
# symbols can override definitions in generic bitcode files.
100+
set( inputs_with_flag )
101+
foreach( file IN LISTS ARG_INPUTS )
102+
string( FIND ${file} "/generic/" is_generic )
103+
if( is_generic LESS 0 )
104+
list( APPEND inputs_with_flag "--override" )
105+
endif()
106+
list( APPEND inputs_with_flag ${file} )
107+
endforeach()
108+
endif()
109+
96110
if( WIN32 OR CYGWIN )
97111
# Create a response file in case the number of inputs exceeds command-line
98112
# character limits on certain platforms.
99113
file( TO_CMAKE_PATH ${LIBCLC_ARCH_OBJFILE_DIR}/${ARG_TARGET}.rsp RSP_FILE )
100114
# Turn it into a space-separate list of input files
101-
list( JOIN ARG_INPUTS " " RSP_INPUT )
115+
list( JOIN inputs_with_flag " " RSP_INPUT )
102116
file( GENERATE OUTPUT ${RSP_FILE} CONTENT ${RSP_INPUT} )
103117
# Ensure that if this file is removed, we re-run CMake
104118
set_property( DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
105119
${RSP_FILE}
106120
)
107121
set( LINK_INPUT_ARG "@${RSP_FILE}" )
122+
else()
123+
set( LINK_INPUT_ARG ${inputs_with_flag} )
108124
endif()
109125

110126
if( ARG_INTERNALIZE )

0 commit comments

Comments
 (0)