@@ -145,13 +145,9 @@ static std::mutex llvm_mutex;
145
145
146
146
void UnlockMutex () { llvm_mutex.unlock (); }
147
147
148
- extern bool ProcessElfInput (STB_TranslateInputArgs &InputArgs, STB_TranslateOutputArgs &OutputArgs,
149
- IGC::OpenCLProgramContext &Context, PLATFORM &platform, const TB_DATA_FORMAT &outType,
150
- float profilingTimerResolution);
151
-
152
- extern bool ParseInput (llvm::Module *&pKernelModule, const STB_TranslateInputArgs *pInputArgs,
153
- STB_TranslateOutputArgs *pOutputArgs, IGC::OpenCLProgramContext &oclContext,
154
- TB_DATA_FORMAT inputDataFormatTemp);
148
+ bool ProcessElfInput (STB_TranslateInputArgs &InputArgs, STB_TranslateOutputArgs &OutputArgs,
149
+ IGC::OpenCLProgramContext &Context, PLATFORM &platform, const TB_DATA_FORMAT &outType,
150
+ float profilingTimerResolution);
155
151
156
152
bool TranslateBuild (const STB_TranslateInputArgs *pInputArgs, STB_TranslateOutputArgs *pOutputArgs,
157
153
TB_DATA_FORMAT inputDataFormatTemp, const IGC::CPlatform &IGCPlatform,
@@ -162,18 +158,16 @@ bool CIGCTranslationBlock::ProcessElfInput(STB_TranslateInputArgs &InputArgs, ST
162
158
return TC::ProcessElfInput (InputArgs, OutputArgs, Context, m_Platform, m_DataFormatOutput, ProfilingTimerResolution);
163
159
}
164
160
165
- static void SetOutputMessage (const std::string &OutputMessage, STB_TranslateOutputArgs &pOutputArgs) {
166
- pOutputArgs.ErrorStringSize = OutputMessage.size () + 1 ;
167
- pOutputArgs.pErrorString = new char [pOutputArgs.ErrorStringSize ];
168
- memcpy_s (pOutputArgs.pErrorString , pOutputArgs.ErrorStringSize , OutputMessage.c_str (), pOutputArgs.ErrorStringSize );
161
+ static void SetOutputMessage (const std::string &OutputMessage, STB_TranslateOutputArgs &OutputArgs) {
162
+ OutputArgs.ErrorString = OutputMessage;
169
163
}
170
164
171
- static void SetWarningMessage (const std::string &OutputMessage, STB_TranslateOutputArgs &pOutputArgs ) {
172
- SetOutputMessage (" warning: " + OutputMessage, pOutputArgs );
165
+ static void SetWarningMessage (const std::string &OutputMessage, STB_TranslateOutputArgs &OutputArgs ) {
166
+ SetOutputMessage (" warning: " + OutputMessage, OutputArgs );
173
167
}
174
168
175
- static void SetErrorMessage (const std::string &OutputMessage, STB_TranslateOutputArgs &pOutputArgs ) {
176
- SetOutputMessage (" error: " + OutputMessage, pOutputArgs );
169
+ static void SetErrorMessage (const std::string &OutputMessage, STB_TranslateOutputArgs &OutputArgs ) {
170
+ SetOutputMessage (" error: " + OutputMessage, OutputArgs );
177
171
}
178
172
179
173
static bool IsDeviceBinaryFormat (const TB_DATA_FORMAT &format) {
@@ -208,12 +202,9 @@ bool CIGCTranslationBlock::Translate(const STB_TranslateInputArgs *pInputArgs, S
208
202
IGC::SetWorkaroundTable (&m_SkuTable, &IGCPlatform);
209
203
IGC::SetCompilerCaps (&m_SkuTable, &IGCPlatform);
210
204
211
- pOutputArgs->pOutput = nullptr ;
212
- pOutputArgs->OutputSize = 0 ;
213
- pOutputArgs->pErrorString = nullptr ;
214
- pOutputArgs->ErrorStringSize = 0 ;
215
- pOutputArgs->pDebugData = nullptr ;
216
- pOutputArgs->DebugDataSize = 0 ;
205
+ pOutputArgs->Output .clear ();
206
+ pOutputArgs->ErrorString .clear ();
207
+ pOutputArgs->DebugData .clear ();
217
208
218
209
try {
219
210
if (m_DataFormatInput == TB_DATA_FORMAT_ELF) {
@@ -243,12 +234,12 @@ bool CIGCTranslationBlock::Translate(const STB_TranslateInputArgs *pInputArgs, S
243
234
return false ;
244
235
}
245
236
} catch (std::exception &e) {
246
- if (pOutputArgs->ErrorStringSize == 0 && pOutputArgs-> pErrorString == nullptr ) {
237
+ if (pOutputArgs->ErrorString . empty () ) {
247
238
SetErrorMessage (std::string (" IGC: " ) + e.what (), *pOutputArgs);
248
239
}
249
240
return false ;
250
241
} catch (...) {
251
- if (pOutputArgs->ErrorStringSize == 0 && pOutputArgs-> pErrorString == nullptr ) {
242
+ if (pOutputArgs->ErrorString . empty () ) {
252
243
SetErrorMessage (" IGC: Internal Compiler Error" , *pOutputArgs);
253
244
}
254
245
return false ;
@@ -654,27 +645,18 @@ bool ProcessElfInput(STB_TranslateInputArgs &InputArgs, STB_TranslateOutputArgs
654
645
if (success == true ) {
655
646
// Now that the output modules are linked the resulting module needs to be
656
647
// serialized out
657
- std::string OutputString;
658
- llvm::raw_string_ostream OStream (OutputString );
648
+ IGC_ASSERT (OutputArgs. Output . empty ()); // Make sure we're not overwriting anything here.
649
+ llvm::raw_svector_ostream OStream (OutputArgs. Output );
659
650
if (OutputModule.get ()) {
660
651
llvm::WriteBitcodeToFile (*OutputModule.get (), OStream);
661
- OStream.flush ();
662
652
} else {
663
653
// OutputModule can be null only if we use visa linking.
664
654
IGC_ASSERT (hasVISALinking);
665
655
}
666
656
667
657
if (outType == TB_DATA_FORMAT_LLVM_BINARY) {
668
658
// Create a copy of the string to return to the caller.
669
- char *pBufResult = static_cast <char *>(operator new (OutputString.size (), std::nothrow));
670
- if (pBufResult != NULL ) {
671
- memcpy_s (pBufResult, OutputString.size (), OutputString.data (), OutputString.size ());
672
- // The buffer is returned to the runtime. When the buffer is not
673
- // needed anymore the runtime ir responsible to call the module for
674
- // destroying it
675
- OutputArgs.OutputSize = OutputString.size ();
676
- OutputArgs.pOutput = pBufResult;
677
-
659
+ if (!OutputArgs.Output .empty ()) {
678
660
#if defined(IGC_SPIRV_ENABLED)
679
661
if (IGC_IS_FLAG_ENABLED (ShaderDumpEnable)) {
680
662
// This part renames the previously dumped SPIR-V files
@@ -686,7 +668,8 @@ bool ProcessElfInput(STB_TranslateInputArgs &InputArgs, STB_TranslateOutputArgs
686
668
oss1 << std::hex << std::setfill (' 0' ) << std::setw (sizeof (prevAsmHash) * CHAR_BIT / 4 ) << prevAsmHash;
687
669
const std::string prevHashString = oss1.str ();
688
670
689
- QWORD newAsmHash = ShaderHashOCL ((const UINT *)OutputArgs.pOutput , OutputArgs.OutputSize / 4 ).getAsmHash ();
671
+ QWORD newAsmHash =
672
+ ShaderHashOCL ((const UINT *)OutputArgs.Output .data (), OutputArgs.Output .size () / 4 ).getAsmHash ();
690
673
std::ostringstream oss2 (std::ostringstream::ate);
691
674
oss2 << std::hex << std::setfill (' 0' ) << std::setw (sizeof (newAsmHash) * CHAR_BIT / 4 ) << newAsmHash;
692
675
const std::string newHashString = oss2.str ();
@@ -723,7 +706,7 @@ bool ProcessElfInput(STB_TranslateInputArgs &InputArgs, STB_TranslateOutputArgs
723
706
// dump the buffer
724
707
FILE *file = fopen (dumpFileName.c_str (), " wb" );
725
708
if (file != NULL ) {
726
- fwrite (pBufResult, OutputString .size (), 1 , file);
709
+ fwrite (OutputArgs. Output . data (), OutputArgs. Output .size (), 1 , file);
727
710
fclose (file);
728
711
}
729
712
} else {
@@ -750,8 +733,10 @@ bool ProcessElfInput(STB_TranslateInputArgs &InputArgs, STB_TranslateOutputArgs
750
733
return false ;
751
734
}
752
735
} else {
753
- InputArgs.pInput = OutputString.data ();
754
- InputArgs.InputSize = OutputString.size ();
736
+ auto OutputIsTheNewInput = std::move (OutputArgs.Output );
737
+ InputArgs.pInput = OutputIsTheNewInput.data ();
738
+ InputArgs.InputSize = OutputIsTheNewInput.size ();
739
+ OutputArgs.Output = OutBufferType (); // Reinitialize pOutput after moving it to InputArgs as pInput.
755
740
success = TC::TranslateBuild (&InputArgs, &OutputArgs, TB_DATA_FORMAT_LLVM_BINARY, Context.platform ,
756
741
profilingTimerResolution);
757
742
InputArgs.pInput = nullptr ;
@@ -767,9 +752,8 @@ bool ProcessElfInput(STB_TranslateInputArgs &InputArgs, STB_TranslateOutputArgs
767
752
return success;
768
753
}
769
754
770
- bool ParseInput (llvm::Module *&pKernelModule, const STB_TranslateInputArgs *pInputArgs,
771
- STB_TranslateOutputArgs *pOutputArgs, llvm::LLVMContext &oclContext, TB_DATA_FORMAT inputDataFormatTemp,
772
- const IGC::CPlatform &IGCPlatform) {
755
+ bool ParseInput (llvm::Module *&pKernelModule, const STB_TranslateInputArgs *pInputArgs, std::string &errorString,
756
+ llvm::LLVMContext &oclContext, TB_DATA_FORMAT inputDataFormatTemp, const IGC::CPlatform &IGCPlatform) {
773
757
pKernelModule = nullptr ;
774
758
775
759
// Parse the module we want to compile
@@ -796,7 +780,7 @@ bool ParseInput(llvm::Module *&pKernelModule, const STB_TranslateInputArgs *pInp
796
780
}
797
781
798
782
if (isLLVM27IR || isLLVM30IR) {
799
- SetErrorMessage ( " Old LLVM IR (possibly from legacy binary) : not supported!" , *pOutputArgs) ;
783
+ errorString = " Old LLVM IR (possibly from legacy binary) : not supported!" ;
800
784
return false ;
801
785
}
802
786
}
@@ -815,15 +799,13 @@ bool ParseInput(llvm::Module *&pKernelModule, const STB_TranslateInputArgs *pInp
815
799
} else if (inputDataFormatTemp == TB_DATA_FORMAT_SPIR_V) {
816
800
#if defined(IGC_SPIRV_ENABLED)
817
801
// convert SPIR-V binary to LLVM module
818
- std::string stringErrMsg;
819
802
bool success =
820
- TranslateSPIRVToLLVM (*pInputArgs, oclContext, strInput, pKernelModule, stringErrMsg , (PLATFORM &)IGCPlatform);
803
+ TranslateSPIRVToLLVM (*pInputArgs, oclContext, strInput, pKernelModule, errorString , (PLATFORM &)IGCPlatform);
821
804
#else
822
805
std::string stringErrMsg{" SPIRV consumption not enabled for the TARGET." };
823
806
bool success = false ;
824
807
#endif
825
808
if (!success) {
826
- SetErrorMessage (stringErrMsg, *pOutputArgs);
827
809
return false ;
828
810
}
829
811
} else {
@@ -836,9 +818,7 @@ bool ParseInput(llvm::Module *&pKernelModule, const STB_TranslateInputArgs *pInp
836
818
if (pKernelModule == nullptr ) {
837
819
err.print (nullptr , llvm::errs (), false );
838
820
IGC_ASSERT_MESSAGE (0 , " Parsing module failed!" );
839
- }
840
- if (pKernelModule == nullptr ) {
841
- SetErrorMessage (" Parsing llvm module failed!" , *pOutputArgs);
821
+ errorString = " Parsing llvm module failed!" ;
842
822
return false ;
843
823
}
844
824
@@ -900,7 +880,7 @@ bool ReadSpecConstantsFromSPIRV(std::istream &IS, std::vector<std::pair<uint32_t
900
880
}
901
881
#endif
902
882
903
- void overrideOCLProgramBinary (OpenCLProgramContext &Ctx, char * &binaryOutput, size_t &binarySize ) {
883
+ void overrideOCLProgramBinary (OpenCLProgramContext &Ctx, OutBufferType &binaryOutput) {
904
884
auto name =
905
885
DumpName (IGC::Debug::GetShaderOutputName ()).Hash (Ctx.hash ).Type (ShaderType::OPENCL_SHADER).Extension (" progbin" );
906
886
@@ -916,14 +896,10 @@ void overrideOCLProgramBinary(OpenCLProgramContext &Ctx, char *&binaryOutput, si
916
896
size_t newBinarySize = (size_t )f.tellg ();
917
897
f.seekg (0 , f.beg );
918
898
919
- char *newBinaryOutput = new char [ newBinarySize] ;
920
- f.read (newBinaryOutput , newBinarySize);
899
+ binaryOutput. resize ( newBinarySize) ;
900
+ f.read (binaryOutput. data () , newBinarySize);
921
901
922
902
IGC_ASSERT_MESSAGE (f.good (), " Not fully read!" );
923
-
924
- delete[] binaryOutput;
925
- binaryOutput = newBinaryOutput;
926
- binarySize = newBinarySize;
927
903
}
928
904
929
905
void dumpOCLProgramBinary (const char *fileName, const char *binaryOutput, size_t binarySize) {
@@ -1103,7 +1079,8 @@ bool TranslateBuildSPMD(const STB_TranslateInputArgs *pInputArgs, STB_TranslateO
1103
1079
DumpShaderFile (pOutputFolder, outputstr.str ().c_str (), outputstr.str ().size (), hash, " _cmd.txt" );
1104
1080
}
1105
1081
1106
- if (!ParseInput (pKernelModule, pInputArgs, pOutputArgs, *llvmContext, inputDataFormatTemp, IGCPlatform)) {
1082
+ if (!ParseInput (pKernelModule, pInputArgs, pOutputArgs->ErrorString , *llvmContext, inputDataFormatTemp,
1083
+ IGCPlatform)) {
1107
1084
return false ;
1108
1085
}
1109
1086
CDriverInfoOCLNEO driverInfoOCL;
@@ -1232,7 +1209,7 @@ bool TranslateBuildSPMD(const STB_TranslateInputArgs *pInputArgs, STB_TranslateO
1232
1209
SetOutputMessage (" IGC: Out Of Memory" , *pOutputArgs);
1233
1210
return false ;
1234
1211
} catch (std::exception &e) {
1235
- if (pOutputArgs->ErrorStringSize == 0 && pOutputArgs-> pErrorString == nullptr ) {
1212
+ if (pOutputArgs->ErrorString . empty () ) {
1236
1213
std::string message = " IGC: " ;
1237
1214
message += oclContext.GetErrorAndWarning ();
1238
1215
message += ' \n ' ;
@@ -1255,8 +1232,8 @@ bool TranslateBuildSPMD(const STB_TranslateInputArgs *pInputArgs, STB_TranslateO
1255
1232
1256
1233
IGC::Debug::RegisterComputeErrHandlers (*oclContext.getLLVMContext ());
1257
1234
1258
- if (!ParseInput (pKernelModule, pInputArgs, pOutputArgs, *oclContext.getLLVMContext (), inputDataFormatTemp ,
1259
- IGCPlatform)) {
1235
+ if (!ParseInput (pKernelModule, pInputArgs, pOutputArgs-> ErrorString , *oclContext.getLLVMContext (),
1236
+ inputDataFormatTemp, IGCPlatform)) {
1260
1237
return false ;
1261
1238
}
1262
1239
oclContext.setModule (pKernelModule);
@@ -1311,11 +1288,7 @@ bool TranslateBuildSPMD(const STB_TranslateInputArgs *pInputArgs, STB_TranslateO
1311
1288
1312
1289
// FIXME: zebin currently only support program output itself, will add debug info
1313
1290
// into it
1314
- size_t binarySize = 0 ;
1315
- char *binaryOutput = nullptr ;
1316
-
1317
- llvm::SmallVector<char , 64 > buf;
1318
- llvm::raw_svector_ostream llvm_os (buf);
1291
+ llvm::raw_svector_ostream llvm_os (pOutputArgs->Output );
1319
1292
const bool excludeIRFromZEBinary =
1320
1293
IGC_IS_FLAG_ENABLED (ExcludeIRFromZEBinary) || oclContext.getModuleMetaData ()->compOpt .ExcludeIRFromZEBinary ;
1321
1294
const char *spv_data = nullptr ;
@@ -1332,24 +1305,16 @@ bool TranslateBuildSPMD(const STB_TranslateInputArgs *pInputArgs, STB_TranslateO
1332
1305
unsigned PtrSzInBits = pKernelModule->getDataLayout ().getPointerSizeInBits ();
1333
1306
unsigned int pointerSizeInBytes = (PtrSzInBits == 64 ) ? 8 : 4 ;
1334
1307
oclContext.m_programOutput .GetZEBinary (llvm_os, pointerSizeInBytes, spv_data, spv_size, metricData, metricDataSize,
1335
- pInputArgs->pOptions , pInputArgs->OptionsSize );
1336
-
1337
- // FIXME: try to avoid memory copy here
1338
- binarySize = buf.size ();
1339
- binaryOutput = new char [binarySize];
1340
- memcpy_s (binaryOutput, binarySize, buf.data (), buf.size ());
1308
+ pInputArgs->pOptions , pInputArgs->OptionsSize );
1341
1309
1342
1310
if (IGC_IS_FLAG_ENABLED (ShaderDumpEnable))
1343
- dumpOCLProgramBinary (oclContext, binaryOutput, binarySize );
1311
+ dumpOCLProgramBinary (oclContext, pOutputArgs-> Output . data (), pOutputArgs-> Output . size () );
1344
1312
1345
1313
if (const char *progbinCustomFN = IGC_GET_REGKEYSTRING (ProgbinDumpFileName))
1346
- dumpOCLProgramBinary (progbinCustomFN, binaryOutput, binarySize );
1314
+ dumpOCLProgramBinary (progbinCustomFN, pOutputArgs-> Output . data (), pOutputArgs-> Output . size () );
1347
1315
1348
1316
if (IGC_IS_FLAG_ENABLED (ShaderOverride))
1349
- overrideOCLProgramBinary (oclContext, binaryOutput, binarySize);
1350
-
1351
- pOutputArgs->OutputSize = binarySize;
1352
- pOutputArgs->pOutput = binaryOutput;
1317
+ overrideOCLProgramBinary (oclContext, pOutputArgs->Output );
1353
1318
1354
1319
COMPILER_TIME_END (&oclContext, TIME_TOTAL);
1355
1320
@@ -1435,12 +1400,6 @@ bool TranslateBuild(const STB_TranslateInputArgs *pInputArgs, STB_TranslateOutpu
1435
1400
return ret;
1436
1401
}
1437
1402
1438
- bool CIGCTranslationBlock::FreeAllocations (STB_TranslateOutputArgs *pOutputArgs) {
1439
- IGC_ASSERT (pOutputArgs);
1440
- delete[] pOutputArgs->pOutput ;
1441
- return true ;
1442
- }
1443
-
1444
1403
bool CIGCTranslationBlock::Initialize (const STB_CreateArgs *pCreateArgs) {
1445
1404
const SGlobalData *pCreateArgsGlobalData = static_cast <const SGlobalData *>(pCreateArgs->pCreateData );
1446
1405
0 commit comments