codegen.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #pragma once
  2. #include <memory>
  3. #include <util/generic/fwd.h>
  4. #include <util/generic/strbuf.h>
  5. class IOutputStream;
  6. namespace llvm {
  7. class Module;
  8. class Function;
  9. class LLVMContext;
  10. class ExecutionEngine;
  11. }
  12. namespace NYql {
  13. namespace NCodegen {
  14. enum class ETarget {
  15. Native, // Run on current processor and OS
  16. CurrentOS,
  17. Linux,
  18. Windows,
  19. Darwin
  20. };
  21. enum class ESanitize {
  22. Auto,
  23. Asan,
  24. Msan,
  25. Tsan
  26. };
  27. struct TCodegenStats {
  28. ui64 TotalFunctions = 0;
  29. ui64 TotalInstructions = 0;
  30. ui64 MaxFunctionInstructions = 0;
  31. };
  32. struct TCompileStats {
  33. ui32 FunctionPassTime = 0;
  34. ui32 ModulePassTime = 0;
  35. ui32 FinalizeTime = 0;
  36. ui64 TotalObjectSize = 0;
  37. };
  38. class ICodegen {
  39. public:
  40. virtual ~ICodegen() = default;
  41. virtual ETarget GetEffectiveTarget() const = 0;
  42. virtual llvm::LLVMContext& GetContext() = 0;
  43. virtual llvm::Module& GetModule() = 0;
  44. virtual llvm::ExecutionEngine& GetEngine() = 0;
  45. virtual void Verify() = 0;
  46. virtual void GetStats(TCodegenStats& stats) = 0;
  47. virtual void ExportSymbol(llvm::Function* function) = 0; // to run DCE before Compile
  48. virtual void Compile(const TStringBuf compileOpts = TStringBuf(), TCompileStats* compileStats = nullptr) = 0;
  49. virtual void* GetPointerToFunction(llvm::Function* function) = 0;
  50. virtual ui64 GetFunctionCodeSize(llvm::Function* function) = 0;
  51. virtual void ShowGeneratedFunctions(IOutputStream* out) = 0;
  52. virtual void LoadBitCode(TStringBuf bitcode, TStringBuf uniqId) = 0;
  53. virtual void AddGlobalMapping(TStringBuf name, const void* address) = 0;
  54. virtual void TogglePerfJITEventListener() = 0;
  55. using TPtr = std::unique_ptr<ICodegen>;
  56. static TPtr Make(ETarget target, ESanitize sanitize = ESanitize::Auto);
  57. using TSharedPtr = std::shared_ptr<ICodegen>;
  58. static TSharedPtr MakeShared(ETarget target, ESanitize sanitize = ESanitize::Auto);
  59. static bool IsCodegenAvailable();
  60. };
  61. }
  62. }