rewriteRuntime.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import re
  2. inputFilename = 'runtime.opt.ll'
  3. sourceFilename = r'C:/Users/chwallis/Desktop/DXILShaderPatch/runtime.c'
  4. outputFilename = 'C:/Users/chwallis/Desktop/DXILShaderPatch/runtime.h'
  5. source = open(sourceFilename).read()
  6. input = open(inputFilename).read()
  7. m = re.search(r'"nvptx"(.*?)attributes #', input, re.DOTALL)
  8. dxil = m.group(1)
  9. # split the string up to avoid error C2026: string too big, trailing characters truncated
  10. lines = dxil.splitlines()
  11. dxil = []
  12. count = 0
  13. for line in lines:
  14. count += len(line)
  15. dxil.append(line)
  16. if count > 10000:
  17. dxil.append(')AAA",')
  18. dxil.append('R"AAA(')
  19. count = 0
  20. dxil = '\n'.join(dxil)
  21. template = """
  22. // This file generated by compiling the following source (runtime.c) as follows:
  23. // clang -S -emit-llvm -target nvptr runtime.c
  24. // opt -S -mem2reg runtime.ll -o runtime.opt.ll
  25. // The resulting LLVM-IR is stripped of its datalayout and replaced with one
  26. // compatible with DXIL.
  27. // runtime.c
  28. #if 0
  29. %SOURCE%
  30. #endif
  31. static const char* runtimeString[] = { R"AAA(
  32. target datalayout = "e-m:e-p:32:32-i1:32-i8:32-i16:32-i32:32-i64:64-f16:32-f32:32-f:64:64-n8:16:32:64"
  33. target triple = "dxil-ms-dx"
  34. %DXIL%
  35. attributes #0 = { nounwind }
  36. attributes #1 = { nounwind }
  37. )AAA"
  38. };
  39. #include <sstream>
  40. static std::string getRuntimeString()
  41. {
  42. std::ostringstream out;
  43. for( size_t i=0; i < _countof(runtimeString); ++i)
  44. out << runtimeString[i];
  45. return out.str();
  46. }
  47. """
  48. output = re.sub(r'%SOURCE%', source, template)
  49. output = re.sub(r'%DXIL%', dxil, output)
  50. open(outputFilename, 'w').write(output)