Skip to content

Commit fabd05c

Browse files
pratikasharigcbot
authored andcommitted
Fold DWARF sub-expression where possible
Simplify DWARF expressions by folding where possible. Also create standard API to decide correct DWARF operation to use based on literal encoding size.
1 parent 9aa3111 commit fabd05c

File tree

3 files changed

+52
-44
lines changed

3 files changed

+52
-44
lines changed

IGC/DebugInfo/DwarfCompileUnit.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,13 +1019,11 @@ void CompileUnit::addBE_FP(IGC::DIEBlock *Block) {
10191019

10201020
addRegOrConst(Block, BE_FP_RegNum); // Register ID will be shifted by offset
10211021

1022-
addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_const2u);
1023-
addUInt(Block, dwarf::DW_FORM_data2,
1024-
BE_FP_SubRegNum * 8u); // sub-reg offset in bits
1022+
addConstantUValue(Block, BE_FP_SubRegNum * 8u); // sub-reg offset in bits
1023+
10251024
extractSubRegValue(Block, 32);
10261025
if (EmitSettings.ScratchOffsetInOW) {
1027-
addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_const1u);
1028-
addUInt(Block, dwarf::DW_FORM_data1, 16);
1026+
addConstantUValue(Block, 16);
10291027
addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_mul);
10301028
}
10311029

@@ -2504,20 +2502,24 @@ bool CompileUnit::buildFpBasedLoc(const DbgVariable &var, IGC::DIEBlock *Block,
25042502
bitOffsetToFPReg); // 2 DW_OP_const1u/2u <bit-offset to Frame Pointer reg>
25052503
extractSubRegValue(Block, 64);
25062504

2507-
addUInt(Block, dwarf::DW_FORM_data1,
2508-
dwarf::DW_OP_plus_uconst); // 5 DW_OP_plus_uconst SIZE_OWORD (taken
2509-
// from getFPOffset())
2510-
addUInt(Block, dwarf::DW_FORM_udata, VISAMod->getFPOffset());
2505+
if (VISAMod->getFPOffset()) {
2506+
addUInt(Block, dwarf::DW_FORM_data1,
2507+
dwarf::DW_OP_plus_uconst); // 5 DW_OP_plus_uconst SIZE_OWORD (taken
2508+
// from getFPOffset())
2509+
addUInt(Block, dwarf::DW_FORM_udata, VISAMod->getFPOffset());
2510+
}
25112511

25122512
addUInt(Block, dwarf::DW_FORM_data1,
25132513
DW_OP_INTEL_push_simd_lane); // 6 DW_OP_INTEL_push_simd_lane
25142514
addConstantUValue(Block, storageSize); // 7 DW_OP_const1u/2u/4u/8u storageSize
25152515
addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_mul); // 8 DW_OP_mul
25162516
addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); // 9 DW_OP_plus
25172517

2518-
addUInt(Block, dwarf::DW_FORM_data1,
2519-
dwarf::DW_OP_plus_uconst); // 10 DW_OP_plus_uconst storageOffset
2520-
addUInt(Block, dwarf::DW_FORM_udata, storageOffset); // storageOffset
2518+
if (storageOffset) {
2519+
addUInt(Block, dwarf::DW_FORM_data1,
2520+
dwarf::DW_OP_plus_uconst); // 10 DW_OP_plus_uconst storageOffset
2521+
addUInt(Block, dwarf::DW_FORM_udata, storageOffset); // storageOffset
2522+
}
25212523

25222524
var.emitExpression(this, Block);
25232525

IGC/DebugInfo/DwarfDebug.cpp

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2784,7 +2784,29 @@ void DwarfDebug::emitDebugMacInfo() {
27842784
}
27852785
}
27862786

2787-
void DwarfDebug::encodeScratchAddrSpace(std::vector<uint8_t> &data) {
2787+
// Given a number value, decide which UConst DWARF opcode
2788+
// should be emitted for minimizing size.
2789+
void DwarfDebug::emitNarrowestUConst(std::vector<uint8_t> &data1,
2790+
uint64_t value) {
2791+
if (value <= 31) {
2792+
write(data1, (uint8_t)llvm::dwarf::DW_OP_lit0 + value);
2793+
} else if (value < 0xff) {
2794+
write(data1, (uint8_t)llvm::dwarf::DW_OP_const1u);
2795+
write(data1, (uint8_t)(value));
2796+
} else if (value < 0xffff) {
2797+
write(data1, (uint8_t)llvm::dwarf::DW_OP_const2u);
2798+
write(data1, (uint16_t)(value));
2799+
} else if (value < 0xffffffff) {
2800+
write(data1, (uint8_t)llvm::dwarf::DW_OP_const4u);
2801+
write(data1, (uint32_t)(value));
2802+
} else {
2803+
write(data1, (uint8_t)llvm::dwarf::DW_OP_const8u);
2804+
write(data1, (uint64_t)(value));
2805+
}
2806+
}
2807+
2808+
void DwarfDebug::encodeScratchAddrSpace(std::vector<uint8_t> &data,
2809+
uint64_t offset) {
27882810
uint32_t scratchBaseAddrEncoded = 0;
27892811
if (m_pModule->usesSlot1ScratchSpill()) {
27902812
scratchBaseAddrEncoded =
@@ -2796,7 +2818,7 @@ void DwarfDebug::encodeScratchAddrSpace(std::vector<uint8_t> &data) {
27962818
}
27972819

27982820
write(data, (uint8_t)scratchBaseAddrEncoded);
2799-
writeULEB128(data, 0);
2821+
writeULEB128(data, offset);
28002822
write(data, (uint8_t)llvm::dwarf::DW_OP_plus);
28012823
}
28022824

@@ -2939,24 +2961,21 @@ uint32_t DwarfDebug::writeStackcallCIE() {
29392961
// The DW_CFA_def_cfa_expression instruction takes a single operand
29402962
// encoded as a DW_FORM_exprloc.
29412963
auto DWRegEncoded = GetEncodedRegNum<RegisterNumbering::GRFBase>(specialGRF);
2942-
write(data1, (uint8_t)llvm::dwarf::DW_OP_const4u);
2943-
write(data1, (uint32_t)(DWRegEncoded));
2944-
write(data1, (uint8_t)llvm::dwarf::DW_OP_const2u);
2945-
write(data1, (uint16_t)(getBEFPSubReg() * 4 * 8));
2964+
emitNarrowestUConst(data1, DWRegEncoded);
2965+
emitNarrowestUConst(data1, getBEFPSubReg() * 4 * 8);
29462966
write(data1, (uint8_t)DW_OP_INTEL_regval_bits);
29472967
write(data1, (uint8_t)32);
29482968

29492969
if (EmitSettings.ScratchOffsetInOW) {
29502970
// when scratch offset is in OW, be_fp has to be multiplied by 16
29512971
// to normalize and generate byte offset for complete address
29522972
// computation.
2953-
write(data1, (uint8_t)llvm::dwarf::DW_OP_const1u);
2954-
write(data1, (uint8_t)16);
2973+
emitNarrowestUConst(data1, 16);
29552974
write(data1, (uint8_t)llvm::dwarf::DW_OP_mul);
29562975
}
29572976

29582977
// indicate that the resulting address is on BE stack
2959-
encodeScratchAddrSpace(data1);
2978+
encodeScratchAddrSpace(data1, 0);
29602979

29612980
writeULEB128(data, data1.size());
29622981
for (auto item : data1)
@@ -3152,51 +3171,37 @@ void DwarfDebug::writeFDEStackCall(VISAModule *m) {
31523171

31533172
auto DWRegEncoded =
31543173
GetEncodedRegNum<RegisterNumbering::GRFBase>(specialGRF);
3155-
write(data1, (uint8_t)llvm::dwarf::DW_OP_const4u);
3156-
write(data1, (uint32_t)(DWRegEncoded));
3157-
write(data1, (uint8_t)llvm::dwarf::DW_OP_const2u);
3158-
write(data1, (uint16_t)(getBEFPSubReg() * 4 * 8));
3174+
emitNarrowestUConst(data1, DWRegEncoded);
3175+
emitNarrowestUConst(data1, getBEFPSubReg() * 4 * 8);
31593176
write(data1, (uint8_t)DW_OP_INTEL_regval_bits);
31603177
write(data1, (uint8_t)32);
31613178

31623179
if (EmitSettings.ScratchOffsetInOW) {
31633180
// when scratch offset is in OW, be_fp has to be multiplied by 16
31643181
// to normalize and generate byte offset for complete address
31653182
// computation.
3166-
write(data1, (uint8_t)llvm::dwarf::DW_OP_const1u);
3167-
write(data1, (uint8_t)16);
3183+
emitNarrowestUConst(data1, 16);
31683184
write(data1, (uint8_t)llvm::dwarf::DW_OP_mul);
31693185
}
31703186

3171-
write(data1, (uint8_t)llvm::dwarf::DW_OP_constu);
3172-
writeULEB128(data1, offset);
3173-
write(data1, (uint8_t)llvm::dwarf::DW_OP_plus);
3174-
31753187
// indicate that the resulting address is on BE stack
3176-
encodeScratchAddrSpace(data1);
3188+
encodeScratchAddrSpace(data1, offset);
31773189

31783190
if (deref) {
3179-
write(data1, (uint8_t)llvm::dwarf::DW_OP_deref);
3180-
// DW_OP_deref reads as many bytes as size of address on target machine.
3181-
// We set address size to 64 bits in CIE. However, this expression
3182-
// refers to a slot in scratch space which uses 32-bit addressing. So
3183-
// mask upper 32 bits read from VISA frame descriptor.
3184-
write(data1, (uint8_t)llvm::dwarf::DW_OP_const4u);
3185-
write(data1, (uint32_t)0xffffffff);
3186-
write(data1, (uint8_t)llvm::dwarf::DW_OP_and);
3191+
write(data1, (uint8_t)llvm::dwarf::DW_OP_deref_size);
3192+
write(data1, (uint8_t)4);
31873193
}
31883194

31893195
if (EmitSettings.ScratchOffsetInOW && normalizeResult) {
31903196
// since data stored in scratch space is also in oword units, normalize it
3191-
write(data1, (uint8_t)llvm::dwarf::DW_OP_const1u);
3192-
write(data1, (uint8_t)16);
3197+
emitNarrowestUConst(data1, 16);
31933198
write(data1, (uint8_t)llvm::dwarf::DW_OP_mul);
31943199
}
31953200

31963201
if (deref) {
31973202
// DW_OP_deref earlier causes CFA to be put on top of dwarf stack.
31983203
// Indicate that the address space of CFA is scratch.
3199-
encodeScratchAddrSpace(data1);
3204+
encodeScratchAddrSpace(data1, 0);
32003205
}
32013206

32023207
writeULEB128(data, data1.size());

IGC/DebugInfo/DwarfDebug.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,8 @@ class DwarfDebug {
748748
private:
749749
void encodeRange(CompileUnit *TheCU, DIE *ScopeDIE,
750750
const llvm::SmallVectorImpl<InsnRange> *Ranges);
751-
void encodeScratchAddrSpace(std::vector<uint8_t> &data);
751+
void emitNarrowestUConst(std::vector<uint8_t> &data1, uint64_t value);
752+
void encodeScratchAddrSpace(std::vector<uint8_t> &data, uint64_t offset);
752753
uint32_t writeSubroutineCIE();
753754
uint32_t writeStackcallCIE();
754755
void writeFDESubroutine(VISAModule *m);

0 commit comments

Comments
 (0)