Skip to content

Commit 2f81f31

Browse files
authored
Fix duplication of global annotation strings in reverse translation (#1204)
This is a patch to avoid duplication of global annotation strings in reverse translation caused by the fact that translator translates each UserSemantic decoration, even if a similar decoration with the same string has also been translated.
1 parent c5b3c8e commit 2f81f31

File tree

4 files changed

+39
-38
lines changed

4 files changed

+39
-38
lines changed

lib/SPIRV/SPIRVReader.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3400,7 +3400,15 @@ void SPIRVToLLVM::transIntelFPGADecorations(SPIRVValue *BV, Value *V) {
34003400
SmallString<256> AnnotStr;
34013401
generateIntelFPGAAnnotation(BV, AnnotStr);
34023402
if (!AnnotStr.empty()) {
3403-
auto *GS = Builder.CreateGlobalStringPtr(AnnotStr);
3403+
Constant *GS = nullptr;
3404+
std::string StringAnnotStr = AnnotStr.c_str();
3405+
auto AnnotItr = AnnotationsMap.find(StringAnnotStr);
3406+
if (AnnotItr != AnnotationsMap.end()) {
3407+
GS = AnnotItr->second;
3408+
} else {
3409+
GS = Builder.CreateGlobalStringPtr(AnnotStr);
3410+
AnnotationsMap.emplace(std::move(StringAnnotStr), GS);
3411+
}
34043412

34053413
Value *BaseInst =
34063414
AL ? Builder.CreateBitCast(V, Int8PtrTyPrivate, V->getName()) : Inst;

lib/SPIRV/SPIRVReader.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,15 @@ class SPIRVToLLVM {
158158
SPIRVBlockToLLVMStructMap BlockMap;
159159
SPIRVToLLVMPlaceholderMap PlaceholderMap;
160160
std::unique_ptr<SPIRVToLLVMDbgTran> DbgTran;
161+
// GlobalAnnotations collects array of annotation entries for global variables
162+
// and functions. They are used in translation of llvm.global.annotations
163+
// instruction.
161164
std::vector<Constant *> GlobalAnnotations;
165+
// AnnotationsMap helps to translate annotation strings for local variables.
166+
// Map values are pointers on global strings in LLVM-IR. It is used to avoid
167+
// duplication of these annotation strings in LLVM-IR, which can be caused by
168+
// multiple translation of UserSemantic decorations with the same literal.
169+
std::unordered_map<std::string, Constant *> AnnotationsMap;
162170

163171
// Loops metadata is translated in the end of a function translation.
164172
// This storage contains pairs of translated loop header basic block and loop

test/transcoding/IntelFPGAMemoryAccesses.ll

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ target triple = "spir64-unknown-unknown"
8181
@.str.5 = private unnamed_addr constant [27 x i8] c"{params:0}{cache-size:127}\00", section "llvm.metadata"
8282
; CHECK-LLVM: [[PARAM_15_CACHE_127:@[a-z0-9_.]+]] = {{.*}}{params:15}{cache-size:127}
8383
@.str.6 = private unnamed_addr constant [28 x i8] c"{params:15}{cache-size:127}\00", section "llvm.metadata"
84-
; TODO: Investigate why the same global annotation string shows up twice in backwards translation.
85-
; CHECK-LLVM: [[PARAM_3_CACHE_0_DOUBLE:@[a-z0-9_.]+]] = {{.*}}{params:3}{cache-size:0}
86-
; CHECK-LLVM: [[PARAM_3_CACHE_0_DOUBLE2:@[a-z0-9_.]+]] = {{.*}}{params:3}{cache-size:0}
87-
; CHECK-LLVM: [[PARAM_12_CACHE_0_DOUBLE:@[a-z0-9_.]+]] = {{.*}}{params:12}
8884

8985
; Function Attrs: norecurse nounwind
9086
define spir_kernel void @_ZTSZ4mainE11fake_kernel() #0 !kernel_arg_addr_space !4 !kernel_arg_access_qual !4 !kernel_arg_type !4 !kernel_arg_base_type !4 !kernel_arg_type_qual !4 {
@@ -206,22 +202,22 @@ entry:
206202
call void @llvm.lifetime.start.p0i8(i64 8, i8* %15) #5
207203
; CHECK-LLVM: %[[FLOAT_FUNC_PARAM_LOAD:[[:alnum:].]+]] = load float addrspace(4)*, float addrspace(4)** %[[FLOAT_FUNC_PARAM]]
208204
; CHECK-LLVM: %[[BITCAST_FLOAT_TO_DOUBLE:[[:alnum:].]+]] = bitcast float addrspace(4)* %[[FLOAT_FUNC_PARAM_LOAD]] to double addrspace(4)*
209-
; CHECK-LLVM: %[[INTRINSIC_CALL:[[:alnum:].]+]] = call double addrspace(4)* @llvm.ptr.annotation.p4f64(double addrspace(4)* %[[BITCAST_FLOAT_TO_DOUBLE]], i8* getelementptr inbounds ({{.*}} [[PARAM_3_CACHE_0_DOUBLE]]
205+
; CHECK-LLVM: %[[INTRINSIC_CALL:[[:alnum:].]+]] = call double addrspace(4)* @llvm.ptr.annotation.p4f64(double addrspace(4)* %[[BITCAST_FLOAT_TO_DOUBLE]], i8* getelementptr inbounds ({{.*}} [[PARAM_3_CACHE_0]]
210206
; CHECK-LLVM: store double addrspace(4)* %[[INTRINSIC_CALL]], double addrspace(4)** %[[DOUBLE_VAR]]
211207
%16 = load float addrspace(4)*, float addrspace(4)** %A.addr, align 8, !tbaa !5
212208
%17 = bitcast float addrspace(4)* %16 to double addrspace(4)*
213209
%18 = call double addrspace(4)* @llvm.ptr.annotation.p4f64(double addrspace(4)* %17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i32 0, i32 0), i32 0, i8* null) #6
214210
store double addrspace(4)* %18, double addrspace(4)** %t, align 8, !tbaa !5
215211
; CHECK-LLVM: %[[FLOAT_FUNC_PARAM_LOAD:[[:alnum:].]+]] = load float addrspace(4)*, float addrspace(4)** %[[FLOAT_FUNC_PARAM]]
216-
; CHECK-LLVM: %[[INTRINSIC_CALL:[[:alnum:].]+]] = call float addrspace(4)* @llvm.ptr.annotation.p4f32(float addrspace(4)* %[[FLOAT_FUNC_PARAM_LOAD]], i8* getelementptr inbounds ({{.*}} [[PARAM_3_CACHE_0_DOUBLE2]]
212+
; CHECK-LLVM: %[[INTRINSIC_CALL:[[:alnum:].]+]] = call float addrspace(4)* @llvm.ptr.annotation.p4f32(float addrspace(4)* %[[FLOAT_FUNC_PARAM_LOAD]], i8* getelementptr inbounds ({{.*}} [[PARAM_3_CACHE_0]]
217213
; CHECK-LLVM: store float 5.000000e+00, float addrspace(4)* %[[INTRINSIC_CALL]]
218214
%19 = load float addrspace(4)*, float addrspace(4)** %A.addr, align 8, !tbaa !5
219215
%20 = call float addrspace(4)* @llvm.ptr.annotation.p4f32(float addrspace(4)* %19, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i32 0, i32 0), i32 0, i8* null) #6
220216
store float 5.000000e+00, float addrspace(4)* %20, align 4, !tbaa !5
221217
%21 = bitcast i32* %s to i8*
222218
call void @llvm.lifetime.start.p0i8(i64 4, i8* %21) #5
223219
; CHECK-LLVM: %[[INT1_FUNC_PARAM_LOAD:[[:alnum:].]+]] = load i32 addrspace(4)*, i32 addrspace(4)** %[[INT_FUNC_PARAM]]
224-
; CHECK-LLVM: %[[INTRINSIC_CALL:[[:alnum:].]+]] = call i32 addrspace(4)* @llvm.ptr.annotation.p4i32(i32 addrspace(4)* %[[INT1_FUNC_PARAM_LOAD]], i8* getelementptr inbounds ({{.*}} [[PARAM_12_CACHE_0_DOUBLE]]
220+
; CHECK-LLVM: %[[INTRINSIC_CALL:[[:alnum:].]+]] = call i32 addrspace(4)* @llvm.ptr.annotation.p4i32(i32 addrspace(4)* %[[INT1_FUNC_PARAM_LOAD]], i8* getelementptr inbounds ({{.*}} [[PARAM_12_CACHE_0]]
225221
; CHECK-LLVM: %[[INTRINSIC_RESULT_LOAD:[[:alnum:].]+]] = load i32, i32 addrspace(4)* %[[INTRINSIC_CALL]]
226222
; CHECK-LLVM: store i32 %[[INTRINSIC_RESULT_LOAD]], i32* %[[INT_VAR_1]]
227223
%22 = load i32 addrspace(4)*, i32 addrspace(4)** %B.addr, align 8, !tbaa !5

0 commit comments

Comments
 (0)