Skip to content

Commit e63aa56

Browse files
DianaChensys_zuul
authored andcommitted
ZEBinary: Support bss section
For preparation of adding zero-initialized global/const variables into bss section. Change-Id: Ie956d6a301d59fa05333db5648b108f7181c3e49
1 parent d485e73 commit e63aa56

File tree

5 files changed

+91
-49
lines changed

5 files changed

+91
-49
lines changed

IGC/AdaptorOCL/OCL/sp/zebin_builder.cpp

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -128,66 +128,73 @@ void ZEBinaryBuilder::addGTPinInfo(const IGC::SOpenCLKernelInfo& annotations)
128128

129129
void ZEBinaryBuilder::addProgramScopeInfo(const IGC::SOpenCLProgramInfo& programInfo)
130130
{
131-
if (hasGlobalConstants(programInfo))
132-
addGlobalConstants(programInfo);
133-
if (hasGlobals(programInfo))
134-
mGlobalSectID = addGlobals(programInfo);
135-
}
136-
137-
bool ZEBinaryBuilder::hasGlobalConstants(const IGC::SOpenCLProgramInfo& annotations)
138-
{
139-
if (!annotations.m_initConstantAnnotation.empty())
140-
return true;
141-
return false;
131+
addGlobalConstants(programInfo);
132+
addGlobals(programInfo);
142133
}
143134

144135
void ZEBinaryBuilder::addGlobalConstants(const IGC::SOpenCLProgramInfo& annotations)
145136
{
137+
if (annotations.m_initConstantAnnotation.empty())
138+
return;
139+
146140
// create a data section for global constant variables
147-
// This function should only be called when hasGlobalConstants returns true
148141
// Two constant data sections: general constants and string literals
149142
IGC_ASSERT(annotations.m_initConstantAnnotation.size() == 2);
150143

151144
// General constants
152-
auto& ca = annotations.m_initConstantAnnotation[0];
153-
uint32_t dataSize = ca->InlineData.size();
154-
uint32_t paddingSize = ca->AllocSize - dataSize;
155-
uint32_t alignment = ca->Alignment;
156-
mGlobalConstSectID = mBuilder.addSectionData("const", (const uint8_t*)ca->InlineData.data(),
157-
dataSize, paddingSize, alignment);
145+
// create a data section for global constant variables
146+
auto& ca = annotations.m_initConstantAnnotation.front();
147+
if (ca->AllocSize) {
148+
uint32_t dataSize = ca->InlineData.size();
149+
uint32_t paddingSize = ca->AllocSize - dataSize;
150+
uint32_t alignment = ca->Alignment;
151+
152+
// FIXME: Before runtime can support bss section, we still generate all zero-initialized variables
153+
// in the .data.const section and no .bss.const section.
154+
// The .bss.const section size is the padding size (ca->AllocSize - ca->InlineData.size()),
155+
// and the normal .data.const size is ca->InlineData.size()
156+
mGlobalConstSectID = mBuilder.addSectionData("const", (const uint8_t*)ca->InlineData.data(),
157+
dataSize, paddingSize, alignment);
158+
}
158159

159160
// String literals
160161
auto& caString = annotations.m_initConstantAnnotation[1];
161162
if (caString->InlineData.size() > 0)
162163
{
163-
dataSize = caString->InlineData.size();
164-
paddingSize = caString->AllocSize - dataSize;
165-
alignment = caString->Alignment;
164+
uint32_t dataSize = caString->InlineData.size();
165+
uint32_t paddingSize = caString->AllocSize - dataSize;
166+
uint32_t alignment = caString->Alignment;
166167
mConstStringSectID = mBuilder.addSectionData("const.string", (const uint8_t*)caString->InlineData.data(),
167168
dataSize, paddingSize, alignment);
168169
}
169170
}
170171

171-
bool ZEBinaryBuilder::hasGlobals(const IGC::SOpenCLProgramInfo& annotations)
172+
void ZEBinaryBuilder::addGlobals(const IGC::SOpenCLProgramInfo& annotations)
172173
{
173-
if (!annotations.m_initGlobalAnnotation.empty())
174-
return true;
175-
return false;
176-
}
177-
174+
if (annotations.m_initGlobalAnnotation.empty())
175+
return;
178176

179-
ZEELFObjectBuilder::SectionID ZEBinaryBuilder::addGlobals(
180-
const IGC::SOpenCLProgramInfo& annotations)
181-
{
182177
// create a data section for global variables
183-
// This function should only be called when hasGlobals return true
184178
// FIXME: not sure in what cases there will be more than one global buffer
185179
IGC_ASSERT(annotations.m_initGlobalAnnotation.size() == 1);
186180
auto& ca = annotations.m_initGlobalAnnotation.front();
181+
182+
if (!ca->AllocSize)
183+
return;
184+
187185
uint32_t dataSize = ca->InlineData.size();
188186
uint32_t paddingSize = ca->AllocSize - dataSize;
189187
uint32_t alignment = ca->Alignment;
190-
return mBuilder.addSectionData("global", (const uint8_t*)ca->InlineData.data(),
188+
189+
// FIXME: Before runtime can support bss section, we still generate all zero-initialized variables
190+
// in the .data.global
191+
// The .bss.global section size is the padding size (ca->AllocSize - ca->InlineData.size()),
192+
// and the normal .data.global size is ca->InlineData.size()
193+
// Side note (when adding bss section):
194+
// mGlobalSectID is the section id that will be referenced by global symbols.
195+
// It should be .data.global if existed. If there's only .bss.global section, then all global
196+
// symbols reference to .bss.global section, so set the mGlobalConstSectID to it
197+
mGlobalSectID = mBuilder.addSectionData("global", (const uint8_t*)ca->InlineData.data(),
191198
dataSize, paddingSize, alignment);
192199
}
193200

IGC/AdaptorOCL/OCL/sp/zebin_builder.hpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,9 @@ class ZEBinaryBuilder : DisallowCopy
9393

9494
/// add data section for global constants
9595
void addGlobalConstants(const IGC::SOpenCLProgramInfo& annotations);
96-
bool hasGlobalConstants(const IGC::SOpenCLProgramInfo& annotations);
9796

9897
/// add data section for globals
99-
zebin::ZEELFObjectBuilder::SectionID addGlobals(
100-
const IGC::SOpenCLProgramInfo& annotations);
101-
bool hasGlobals(const IGC::SOpenCLProgramInfo& annotations);
98+
void addGlobals(const IGC::SOpenCLProgramInfo& annotations);
10299

103100
/// add spir-v section
104101
void addSPIRV(const uint8_t* data, uint32_t size);
@@ -172,9 +169,9 @@ class ZEBinaryBuilder : DisallowCopy
172169

173170
/// sectionID holder for program scope sections
174171
/// There should be only one global, global constant buffer per program
175-
zebin::ZEELFObjectBuilder::SectionID mGlobalConstSectID;
176-
zebin::ZEELFObjectBuilder::SectionID mConstStringSectID;
177-
zebin::ZEELFObjectBuilder::SectionID mGlobalSectID;
172+
zebin::ZEELFObjectBuilder::SectionID mGlobalConstSectID = -1;
173+
zebin::ZEELFObjectBuilder::SectionID mConstStringSectID = -1;
174+
zebin::ZEELFObjectBuilder::SectionID mGlobalSectID = -1;
178175
};
179176

180-
} //namespace iOpenCL
177+
} //namespace iOpenCL

IGC/ZEBinWriter/zebin/source/ZEELFObjectBuilder.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,23 @@ ZEELFObjectBuilder::addSectionData(
207207
sectName = m_DataName;
208208

209209
Section& sect = addStandardSection(name, sectName, data, size, ELF::SHT_PROGBITS,
210-
padding, align, m_dataSections);
210+
padding, align, m_dataAndbssSections);
211+
return sect.id();
212+
}
213+
214+
ZEELFObjectBuilder::SectionID
215+
ZEELFObjectBuilder::addSectionBss(
216+
std::string name, uint64_t size, uint32_t padding, uint32_t align)
217+
{
218+
// adjust the section name to be .bss.givenSectName
219+
std::string sectName;
220+
if (name != "")
221+
sectName = m_BssName + "." + name;
222+
else
223+
sectName = m_BssName;
224+
225+
Section& sect = addStandardSection(name, sectName, nullptr, size, ELF::SHT_NOBITS,
226+
padding, align, m_dataAndbssSections);
211227
return sect.id();
212228
}
213229

@@ -310,7 +326,7 @@ std::string ZEELFObjectBuilder::getSectionNameBySectionID(SectionID id)
310326
if (sect.id() == id)
311327
return sect.m_name;
312328
}
313-
for (StandardSection& sect : m_dataSections) {
329+
for (StandardSection& sect : m_dataAndbssSections) {
314330
if (sect.id() == id)
315331
return sect.m_name;
316332
}
@@ -516,11 +532,18 @@ void ELFWriter::writeSections()
516532
IGC_ASSERT(entry.section->getKind() == Section::STANDARD);
517533
const StandardSection* const stdsect =
518534
static_cast<const StandardSection*>(entry.section);
519-
IGC_ASSERT(nullptr != stdsect);
535+
IGC_ASSERT(nullptr != stdsect && nullptr != stdsect->m_data);
520536
entry.size = writeSectionData(
521537
stdsect->m_data, stdsect->m_size, stdsect->m_padding);
522538
break;
523539
}
540+
case ELF::SHT_NOBITS: {
541+
const StandardSection* const stdsect =
542+
static_cast<const StandardSection*>(entry.section);
543+
IGC_ASSERT(nullptr != stdsect);
544+
entry.size = stdsect->m_size;
545+
break;
546+
}
524547
case ELF::SHT_SYMTAB:
525548
entry.size = writeSymTab();
526549
entry.entsize = getSymTabEntSize();
@@ -529,6 +552,7 @@ void ELFWriter::writeSections()
529552
// first null symbol
530553
entry.info = m_ObjBuilder.m_localSymbols.size() + 1;
531554
break;
555+
532556
case ELF::SHT_REL: {
533557
IGC_ASSERT(nullptr != entry.section);
534558
IGC_ASSERT(entry.section->getKind() == Section::RELOC);
@@ -706,7 +730,7 @@ void ELFWriter::createSectionHdrEntries()
706730
}
707731

708732
// .data
709-
for (StandardSection& sect : m_ObjBuilder.m_dataSections) {
733+
for (StandardSection& sect : m_ObjBuilder.m_dataAndbssSections) {
710734
m_SectionIndex.insert(std::make_pair(sect.id(), index));
711735
++index;
712736
createSectionHdrEntry(sect.m_sectName, sect.m_type, &sect);

IGC/ZEBinWriter/zebin/source/ZEELFObjectBuilder.hpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ namespace zebin {
6060
class ZEELFObjectBuilder {
6161
friend class ELFWriter;
6262
public:
63-
typedef uint32_t SectionID;
63+
// The valid SectionID must be 0 or positive value
64+
typedef int32_t SectionID;
6465

6566
public:
6667
// Each ZEELFObjectBuilder creates one ELF Object
@@ -127,11 +128,21 @@ class ZEELFObjectBuilder {
127128
// - padding: required padding at the end
128129
// - align: alignment requirement in byte
129130
// - return a unique id for referencing in addSymbol
130-
// - return a unique id for referencing in addSymbol
131131
SectionID addSectionData(
132132
std::string name, const uint8_t* data, uint64_t size, uint32_t padding = 0, uint32_t align = 0);
133133

134-
// add a data section contains raw data, such as constant or global buffer.
134+
// add a bss section which occupies no space in the ELF, but with size and other section information
135+
// The bss sections could be used for zero-initialized variables.
136+
// - name: section name. Do not includes leading .bss in given name.
137+
// For example, giving "const", the section name will be .bss.const
138+
// - size: input buffer size in byte
139+
// - padding: required padding at the end
140+
// - align: alignment requirement in byte
141+
// - return a unique id for referencing in addSymbol
142+
SectionID addSectionBss(
143+
std::string name, uint64_t size, uint32_t padding = 0, uint32_t align = 0);
144+
145+
// add a spv section contains the spv source. This section will be set to SHT_ZEBIN_SPIRV type
135146
// - name: section name. The default name is .spv
136147
// - size in byte
137148
// - Note that the alignment requirement of the section should be satisfied
@@ -325,6 +336,7 @@ class ZEELFObjectBuilder {
325336
// place holder for section default name
326337
const std::string m_TextName = ".text";
327338
const std::string m_DataName = ".data";
339+
const std::string m_BssName = ".bss";
328340
const std::string m_SymTabName = ".symtab";
329341
const std::string m_RelName = ".rel";
330342
const std::string m_SpvName = ".spv";
@@ -343,12 +355,12 @@ class ZEELFObjectBuilder {
343355
TargetFlags m_flags; // e_flags
344356

345357
StandardSectionListTy m_textSections;
346-
StandardSectionListTy m_dataSections;
358+
StandardSectionListTy m_dataAndbssSections; // data and bss sections
347359
StandardSectionListTy m_otherStdSections;
348360
RelocSectionListTy m_relocSections;
349361

350362
// current section id
351-
uint32_t m_sectionId = 0;
363+
SectionID m_sectionId = 0;
352364

353365
// every ze object contains only one ze_info section
354366
ZEInfoSection* m_zeInfoSection = nullptr;

IGC/ZEBinWriter/zebin/spec/elf.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
| Section headers | Standard ELF section headers ||
77
| .text.{*kernel_name*} | Gen binary of kernel/functions in this compiled module | SHT_PROGBITS |
88
| .data.const | Constant data section (if any) | SHT_PROGBITS |
9+
| .bss.const | Constant data with zero-initialized variables (if any) | SHT_NOBITS |
910
| .data.global | Global data section (if any) | SHT_PROGBITS |
11+
| .bss.global | Global data with zero-initialized variables (if any) | SHT_NOBITS |
1012
| .symtab | Symbol table (if any) | SHT_SYMTAB |
1113
| .rel.{*kernel_name*} | Relocation table (if any) | SHT_REL |
1214
| .spv | Spir-v of the module (if required) | SHT_ZEBIN_SPIRV |

0 commit comments

Comments
 (0)