Skip to content

Commit 6edbfe1

Browse files
LU-JOHNsys-ce-bb
LU-JOHN
authored andcommitted
Round up #elts of TypeVector when calculating memory size (#2504)
Round up number of elements in a Vector to a power of 2 when calculating memory size. Memory size will be calculated as BaseType * bit_ceil(ComponentCount). The previous calculation already rounded 3 elements to 4 elements. Signed-off-by: Lu, John <[email protected]> Original commit: KhronosGroup/SPIRV-LLVM-Translator@a6398f45674bb5d
1 parent ec826c5 commit 6edbfe1

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

llvm-spirv/lib/SPIRV/SPIRVToLLVMDbgTran.cpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "llvm/IR/DebugProgramInstruction.h"
5151
#include "llvm/IR/IntrinsicInst.h"
5252
#include "llvm/IR/Module.h"
53+
#include <bit>
5354

5455
using namespace std;
5556
using namespace SPIRVDebug::Operand;
@@ -484,15 +485,13 @@ SPIRVToLLVMDbgTran::transTypeVector(const SPIRVExtInst *DebugInst) {
484485
transNonNullDebugType(BM->get<SPIRVExtInst>(Ops[BaseTypeIdx]));
485486
SPIRVWord Count = getConstantValueOrLiteral(Ops, ComponentCountIdx,
486487
DebugInst->getExtSetKind());
487-
// FIXME: The current design of SPIR-V Debug Info doesn't provide a field
488-
// for the derived memory size. Meanwhile, OpenCL/SYCL 3-element vectors
489-
// occupy the same amount of memory as 4-element vectors, hence the simple
490-
// elem_count * elem_size formula fails in this edge case.
491-
// Once the specification is updated to reflect the whole memory block's
492-
// size in SPIR-V, the calculations below must be replaced with a simple
493-
// translation of the known size.
494-
SPIRVWord SizeCount = (Count == 3) ? 4 : Count;
495-
uint64_t Size = getDerivedSizeInBits(BaseTy) * SizeCount;
488+
// Round up to a power of two.
489+
// OpenCL/SYCL 3-element vectors
490+
// occupy the same amount of memory as 4-element vectors
491+
// Clang rounds up the memory size of vectors to a power of 2.
492+
// Vulkan allows vec3 to have a memory size of 12, but in RenderDoc memory
493+
// size is not derived from debug info.
494+
uint64_t Size = getDerivedSizeInBits(BaseTy) * bit_ceil(Count);
496495

497496
SmallVector<llvm::Metadata *, 8> Subscripts;
498497
Subscripts.push_back(getDIBuilder(DebugInst).getOrCreateSubrange(0, Count));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
; Ensure that a vector type's memory size is calculated as bit_ceil(# elements) * element size
2+
; even if the (# elements) is not 3.
3+
;
4+
; This test was derived from DebugInfo/X86/sycl-vec-3.ll.
5+
6+
; RUN: llvm-as < %s -o %t.bc
7+
8+
; RUN: llvm-spirv %t.bc -o %t.spv -spirv-ext=+SPV_INTEL_vector_compute
9+
; RUN: llvm-spirv -r %t.spv -o %t.bc
10+
; RUN: llvm-dis %t.bc -o - | FileCheck %s --check-prefixes=CHECK
11+
12+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
13+
target triple = "spir64-unknown-unknown"
14+
15+
%"class.cl::sycl::vec" = type { <832 x i32> }
16+
@vector = dso_local addrspace(1) global %"class.cl::sycl::vec" zeroinitializer, align 16, !dbg !0
17+
18+
!llvm.dbg.cu = !{!9}
19+
!llvm.module.flags = !{!10, !11, !12, !13, !14}
20+
21+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
22+
!1 = distinct !DIGlobalVariable(name: "vector", scope: null, file: !2, line: 3, type: !3, isLocal: false, isDefinition: true)
23+
!2 = !DIFile(filename: "sycl-vec-3.cpp", directory: "/tmp")
24+
; CHECK: !DICompositeType(tag: DW_TAG_array_type, baseType: ![[BASE_TY:[0-9]+]],{{.*}} size: 32768, flags: DIFlagVector, elements: ![[ELEMS:[0-9]+]])
25+
!3 = distinct !DICompositeType(tag: DW_TAG_array_type, baseType: !6, file: !2, line: 3, size: 32768, flags: DIFlagVector, elements: !4, identifier: "_ZTSN2cl4sycl3vecIiLi3EEE")
26+
; CHECK-DAG: ![[ELEMS]] = !{![[ELEMS_RANGE:[0-9]+]]}
27+
!4 = !{!5}
28+
; CHECK-DAG: ![[ELEMS_RANGE]] = !DISubrange(count: 832{{.*}})
29+
!5 = !DISubrange(count: 832)
30+
; CHECK-DAG: ![[BASE_TY]] = !DIBasicType(name: "int", size: 32,{{.*}} encoding: DW_ATE_signed)
31+
!6 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
32+
!7 = !{}
33+
!8 = !{!0}
34+
!9 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !2, producer: "clang version 13.0.0 (https://github.com/intel/llvm.git)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !7, retainedTypes: !7, globals: !8, imports: !7)
35+
!10 = !{i32 7, !"Dwarf Version", i32 4}
36+
!11 = !{i32 2, !"Debug Info Version", i32 3}
37+
!12 = !{i32 1, !"wchar_size", i32 4}
38+
!13 = !{i32 7, !"uwtable", i32 1}
39+
!14 = !{i32 7, !"frame-pointer", i32 2}

0 commit comments

Comments
 (0)