38
38
#include " llvm/Frontend/OpenMP/OMPConstants.h"
39
39
#include " llvm/Frontend/OpenMP/OMPGridValues.h"
40
40
#include " llvm/Support/Error.h"
41
+ #include " llvm/Support/FileOutputBuffer.h"
41
42
#include " llvm/Support/FileSystem.h"
42
43
#include " llvm/Support/MemoryBuffer.h"
43
44
#include " llvm/Support/Program.h"
@@ -1999,21 +2000,27 @@ struct AMDGPUDeviceTy : public GenericDeviceTy, AMDGenericDeviceTy {
1999
2000
2000
2001
// TODO: We should try to avoid materialization but there seems to be no
2001
2002
// good linker interface w/o file i/o.
2002
- SmallString<128 > LinkerOutputFilePath ;
2003
- std::error_code EC = sys::fs::createTemporaryFile (
2004
- " amdgpu-pre-link-jit " , " .out " , LinkerOutputFilePath );
2003
+ SmallString<128 > LinkerInputFilePath ;
2004
+ std::error_code EC = sys::fs::createTemporaryFile (" amdgpu-pre-link-jit " ,
2005
+ " o " , LinkerInputFilePath );
2005
2006
if (EC)
2006
- return createStringError (EC,
2007
- " Failed to create temporary file for linker" );
2008
-
2009
- SmallString<128 > LinkerInputFilePath = LinkerOutputFilePath;
2010
- LinkerInputFilePath.pop_back_n (2 );
2007
+ return Plugin::error (" Failed to create temporary file for linker" );
2008
+
2009
+ // Write the file's contents to the output file.
2010
+ Expected<std::unique_ptr<FileOutputBuffer>> OutputOrErr =
2011
+ FileOutputBuffer::create (LinkerInputFilePath, MB->getBuffer ().size ());
2012
+ if (!OutputOrErr)
2013
+ return OutputOrErr.takeError ();
2014
+ std::unique_ptr<FileOutputBuffer> Output = std::move (*OutputOrErr);
2015
+ llvm::copy (MB->getBuffer (), Output->getBufferStart ());
2016
+ if (Error E = Output->commit ())
2017
+ return std::move (E);
2011
2018
2012
- auto FD = raw_fd_ostream (LinkerInputFilePath.data (), EC);
2019
+ SmallString<128 > LinkerOutputFilePath;
2020
+ EC = sys::fs::createTemporaryFile (" amdgpu-pre-link-jit" , " so" ,
2021
+ LinkerOutputFilePath);
2013
2022
if (EC)
2014
- return createStringError (EC, " Failed to open temporary file for linker" );
2015
- FD.write (MB->getBufferStart (), MB->getBufferSize ());
2016
- FD.close ();
2023
+ return Plugin::error (" Failed to create temporary file for linker" );
2017
2024
2018
2025
const auto &ErrorOrPath = sys::findProgramByName (" lld" );
2019
2026
if (!ErrorOrPath)
@@ -2025,7 +2032,6 @@ struct AMDGPUDeviceTy : public GenericDeviceTy, AMDGenericDeviceTy {
2025
2032
" Using `%s` to link JITed amdgcn ouput." , LLDPath.c_str ());
2026
2033
2027
2034
std::string MCPU = " -plugin-opt=mcpu=" + getComputeUnitKind ();
2028
-
2029
2035
StringRef Args[] = {LLDPath,
2030
2036
" -flavor" ,
2031
2037
" gnu" ,
@@ -2039,12 +2045,20 @@ struct AMDGPUDeviceTy : public GenericDeviceTy, AMDGenericDeviceTy {
2039
2045
std::string Error;
2040
2046
int RC = sys::ExecuteAndWait (LLDPath, Args, std::nullopt, {}, 0 , 0 , &Error);
2041
2047
if (RC)
2042
- return createStringError (inconvertibleErrorCode (),
2043
- " Linking optimized bitcode failed: %s" ,
2044
- Error.c_str ());
2048
+ return Plugin::error (" Linking optimized bitcode failed: %s" ,
2049
+ Error.c_str ());
2050
+
2051
+ auto BufferOrErr = MemoryBuffer::getFileOrSTDIN (LinkerOutputFilePath);
2052
+ if (!BufferOrErr)
2053
+ return Plugin::error (" Failed to open temporary file for lld" );
2054
+
2055
+ // Clean up the temporary files afterwards.
2056
+ if (sys::fs::remove (LinkerOutputFilePath))
2057
+ return Plugin::error (" Failed to remove temporary output file for lld" );
2058
+ if (sys::fs::remove (LinkerInputFilePath))
2059
+ return Plugin::error (" Failed to remove temporary input file for lld" );
2045
2060
2046
- return std::move (
2047
- MemoryBuffer::getFileOrSTDIN (LinkerOutputFilePath.data ()).get ());
2061
+ return std::move (*BufferOrErr);
2048
2062
}
2049
2063
2050
2064
// / See GenericDeviceTy::getComputeUnitKind().
0 commit comments