Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. //
  2. // Copyright 2012 Francisco Jerez
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a
  5. // copy of this software and associated documentation files (the "Software"),
  6. // to deal in the Software without restriction, including without limitation
  7. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. // and/or sell copies of the Software, and to permit persons to whom the
  9. // Software is furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  17. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. // OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22.  
  23. #include "core/compiler.hpp"
  24.  
  25. #include <clang/Frontend/CompilerInstance.h>
  26. #include <clang/Frontend/TextDiagnosticBuffer.h>
  27. #include <clang/Frontend/TextDiagnosticPrinter.h>
  28. #include <clang/CodeGen/CodeGenAction.h>
  29. #include <llvm/Bitcode/BitstreamWriter.h>
  30. #include <llvm/Bitcode/ReaderWriter.h>
  31. #include <llvm/Linker.h>
  32. #if HAVE_LLVM < 0x0303
  33. #include <llvm/DerivedTypes.h>
  34. #include <llvm/LLVMContext.h>
  35. #include <llvm/Module.h>
  36. #else
  37. #include <llvm/IR/DerivedTypes.h>
  38. #include <llvm/IR/LLVMContext.h>
  39. #include <llvm/IR/Module.h>
  40. #include <llvm/Support/SourceMgr.h>
  41. #include <llvm/IRReader/IRReader.h>
  42. #endif
  43. #include <llvm/PassManager.h>
  44. #include <llvm/Support/TargetSelect.h>
  45. #include <llvm/Support/MemoryBuffer.h>
  46. #if HAVE_LLVM < 0x0303
  47. #include <llvm/Support/PathV1.h>
  48. #endif
  49. #include <llvm/Transforms/IPO.h>
  50. #include <llvm/Transforms/IPO/PassManagerBuilder.h>
  51.  
  52. #if HAVE_LLVM < 0x0302
  53. #include <llvm/Target/TargetData.h>
  54. #elif HAVE_LLVM < 0x0303
  55. #include <llvm/DataLayout.h>
  56. #else
  57. #include <llvm/IR/DataLayout.h>
  58. #endif
  59.  
  60. #include "pipe/p_state.h"
  61. #include "util/u_memory.h"
  62.  
  63. #include <iostream>
  64. #include <iomanip>
  65. #include <fstream>
  66. #include <cstdio>
  67. #include <sstream>
  68.  
  69. using namespace clover;
  70.  
  71. namespace {
  72. #if 0
  73.    void
  74.    build_binary(const std::string &source, const std::string &target,
  75.                 const std::string &name) {
  76.       clang::CompilerInstance c;
  77.       clang::EmitObjAction act(&llvm::getGlobalContext());
  78.       std::string log;
  79.       llvm::raw_string_ostream s_log(log);
  80.  
  81.       LLVMInitializeTGSITarget();
  82.       LLVMInitializeTGSITargetInfo();
  83.       LLVMInitializeTGSITargetMC();
  84.       LLVMInitializeTGSIAsmPrinter();
  85.  
  86.       c.getFrontendOpts().Inputs.push_back(
  87.          std::make_pair(clang::IK_OpenCL, name));
  88.       c.getHeaderSearchOpts().UseBuiltinIncludes = false;
  89.       c.getHeaderSearchOpts().UseStandardIncludes = false;
  90.       c.getLangOpts().NoBuiltin = true;
  91.       c.getTargetOpts().Triple = target;
  92.       c.getInvocation().setLangDefaults(clang::IK_OpenCL);
  93.       c.createDiagnostics(0, NULL, new clang::TextDiagnosticPrinter(
  94.                              s_log, c.getDiagnosticOpts()));
  95.  
  96.       c.getPreprocessorOpts().addRemappedFile(
  97.          name, llvm::MemoryBuffer::getMemBuffer(source));
  98.  
  99.       if (!c.ExecuteAction(act))
  100.          throw build_error(log);
  101.    }
  102.  
  103.    module
  104.    load_binary(const char *name) {
  105.       std::ifstream fs((name));
  106.       std::vector<unsigned char> str((std::istreambuf_iterator<char>(fs)),
  107.                                      (std::istreambuf_iterator<char>()));
  108.       compat::istream cs(str);
  109.       return module::deserialize(cs);
  110.    }
  111. #endif
  112.  
  113.    llvm::Module *
  114.    compile(const std::string &source, const std::string &name,
  115.            const std::string &triple, const std::string &processor,
  116.            const std::string &opts) {
  117.  
  118.       clang::CompilerInstance c;
  119.       clang::CompilerInvocation invocation;
  120.       clang::EmitLLVMOnlyAction act(&llvm::getGlobalContext());
  121.       std::string log;
  122.       llvm::raw_string_ostream s_log(log);
  123.  
  124.       // Parse the compiler options:
  125.       std::vector<std::string> opts_array;
  126.       std::istringstream ss(opts);
  127.  
  128.       while (!ss.eof()) {
  129.          std::string opt;
  130.          getline(ss, opt, ' ');
  131.          opts_array.push_back(opt);
  132.       }
  133.  
  134.       opts_array.push_back(name);
  135.  
  136.       std::vector<const char *> opts_carray;
  137.       for (unsigned i = 0; i < opts_array.size(); i++) {
  138.          opts_carray.push_back(opts_array.at(i).c_str());
  139.       }
  140.  
  141.       llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID;
  142.       llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagOpts;
  143.       clang::TextDiagnosticBuffer *DiagsBuffer;
  144.  
  145.       DiagID = new clang::DiagnosticIDs();
  146.       DiagOpts = new clang::DiagnosticOptions();
  147.       DiagsBuffer = new clang::TextDiagnosticBuffer();
  148.  
  149.       clang::DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
  150.       bool Success;
  151.  
  152.       Success = clang::CompilerInvocation::CreateFromArgs(c.getInvocation(),
  153.                                         opts_carray.data(),
  154.                                         opts_carray.data() + opts_carray.size(),
  155.                                         Diags);
  156.       if (!Success) {
  157.          throw invalid_option_error();
  158.       }
  159.       c.getFrontendOpts().ProgramAction = clang::frontend::EmitLLVMOnly;
  160.       c.getHeaderSearchOpts().UseBuiltinIncludes = true;
  161.       c.getHeaderSearchOpts().UseStandardSystemIncludes = true;
  162.       c.getHeaderSearchOpts().ResourceDir = CLANG_RESOURCE_DIR;
  163.  
  164.       // Add libclc generic search path
  165.       c.getHeaderSearchOpts().AddPath(LIBCLC_INCLUDEDIR,
  166.                                       clang::frontend::Angled,
  167.                                       false, false
  168. #if HAVE_LLVM < 0x0303
  169.                                       , false
  170. #endif
  171.                                       );
  172.  
  173.       // Add libclc include
  174.       c.getPreprocessorOpts().Includes.push_back("clc/clc.h");
  175.  
  176.       // clc.h requires that this macro be defined:
  177.       c.getPreprocessorOpts().addMacroDef("cl_clang_storage_class_specifiers");
  178.  
  179.       c.getLangOpts().NoBuiltin = true;
  180.       c.getTargetOpts().Triple = triple;
  181.       c.getTargetOpts().CPU = processor;
  182. #if HAVE_LLVM <= 0x0301
  183.       c.getInvocation().setLangDefaults(clang::IK_OpenCL);
  184. #else
  185.       c.getInvocation().setLangDefaults(c.getLangOpts(), clang::IK_OpenCL,
  186.                                         clang::LangStandard::lang_opencl11);
  187. #endif
  188.       c.createDiagnostics(
  189. #if HAVE_LLVM < 0x0303
  190.                           0, NULL,
  191. #endif
  192.                           new clang::TextDiagnosticPrinter(
  193.                                  s_log,
  194. #if HAVE_LLVM <= 0x0301
  195.                                  c.getDiagnosticOpts()));
  196. #else
  197.                                  &c.getDiagnosticOpts()));
  198. #endif
  199.  
  200.       c.getPreprocessorOpts().addRemappedFile(name,
  201.                                       llvm::MemoryBuffer::getMemBuffer(source));
  202.  
  203.       // Compile the code
  204.       if (!c.ExecuteAction(act))
  205.          throw build_error(log);
  206.  
  207.       return act.takeModule();
  208.    }
  209.  
  210.    void
  211.    find_kernels(llvm::Module *mod, std::vector<llvm::Function *> &kernels) {
  212.       const llvm::NamedMDNode *kernel_node =
  213.                                  mod->getNamedMetadata("opencl.kernels");
  214.       // This means there are no kernels in the program.  The spec does not
  215.       // require that we return an error here, but there will be an error if
  216.       // the user tries to pass this program to a clCreateKernel() call.
  217.       if (!kernel_node) {
  218.          return;
  219.       }
  220.  
  221.       for (unsigned i = 0; i < kernel_node->getNumOperands(); ++i) {
  222.          kernels.push_back(llvm::dyn_cast<llvm::Function>(
  223.                                     kernel_node->getOperand(i)->getOperand(0)));
  224.       }
  225.    }
  226.  
  227.    void
  228.    link(llvm::Module *mod, const std::string &triple,
  229.         const std::string &processor,
  230.         const std::vector<llvm::Function *> &kernels) {
  231.  
  232.       llvm::PassManager PM;
  233.       llvm::PassManagerBuilder Builder;
  234.       std::string libclc_path = LIBCLC_LIBEXECDIR + processor + "-"
  235.                                                   + triple + ".bc";
  236.       // Link the kernel with libclc
  237. #if HAVE_LLVM < 0x0303
  238.       bool isNative;
  239.       llvm::Linker linker("clover", mod);
  240.       linker.LinkInFile(llvm::sys::Path(libclc_path), isNative);
  241.       mod = linker.releaseModule();
  242. #else
  243.       std::string err_str;
  244.       llvm::SMDiagnostic err;
  245.       llvm::Module *libclc_mod = llvm::ParseIRFile(libclc_path, err,
  246.                                                    mod->getContext());
  247.       if (llvm::Linker::LinkModules(mod, libclc_mod,
  248.                                     llvm::Linker::DestroySource,
  249.                                     &err_str)) {
  250.          throw build_error(err_str);
  251.       }
  252. #endif
  253.  
  254.       // Add a function internalizer pass.
  255.       //
  256.       // By default, the function internalizer pass will look for a function
  257.       // called "main" and then mark all other functions as internal.  Marking
  258.       // functions as internal enables the optimizer to perform optimizations
  259.       // like function inlining and global dead-code elimination.
  260.       //
  261.       // When there is no "main" function in a module, the internalize pass will
  262.       // treat the module like a library, and it won't internalize any functions.
  263.       // Since there is no "main" function in our kernels, we need to tell
  264.       // the internalizer pass that this module is not a library by passing a
  265.       // list of kernel functions to the internalizer.  The internalizer will
  266.       // treat the functions in the list as "main" functions and internalize
  267.       // all of the other functions.
  268.       std::vector<const char*> export_list;
  269.       for (std::vector<llvm::Function *>::const_iterator I = kernels.begin(),
  270.                                                          E = kernels.end();
  271.                                                          I != E; ++I) {
  272.          llvm::Function *kernel = *I;
  273.          export_list.push_back(kernel->getName().data());
  274.       }
  275.       PM.add(llvm::createInternalizePass(export_list));
  276.  
  277.       // Run link time optimizations
  278.       Builder.OptLevel = 2;
  279.       Builder.populateLTOPassManager(PM, false, true);
  280.       PM.run(*mod);
  281.    }
  282.  
  283.    module
  284.    build_module_llvm(llvm::Module *mod,
  285.                      const std::vector<llvm::Function *> &kernels) {
  286.  
  287.       module m;
  288.       struct pipe_llvm_program_header header;
  289.  
  290.       llvm::SmallVector<char, 1024> llvm_bitcode;
  291.       llvm::raw_svector_ostream bitcode_ostream(llvm_bitcode);
  292.       llvm::BitstreamWriter writer(llvm_bitcode);
  293.       llvm::WriteBitcodeToFile(mod, bitcode_ostream);
  294.       bitcode_ostream.flush();
  295.  
  296.       for (unsigned i = 0; i < kernels.size(); ++i) {
  297.          llvm::Function *kernel_func;
  298.          std::string kernel_name;
  299.          compat::vector<module::argument> args;
  300.  
  301.          kernel_func = kernels[i];
  302.          kernel_name = kernel_func->getName();
  303.  
  304.          for (llvm::Function::arg_iterator I = kernel_func->arg_begin(),
  305.                                       E = kernel_func->arg_end(); I != E; ++I) {
  306.             llvm::Argument &arg = *I;
  307. #if HAVE_LLVM < 0x0302
  308.             llvm::TargetData TD(kernel_func->getParent());
  309. #else
  310.             llvm::DataLayout TD(kernel_func->getParent()->getDataLayout());
  311. #endif
  312.  
  313.             llvm::Type *arg_type = arg.getType();
  314.             unsigned arg_size = TD.getTypeStoreSize(arg_type);
  315.  
  316.             llvm::Type *target_type = arg_type->isIntegerTy() ?
  317.                TD.getSmallestLegalIntType(mod->getContext(), arg_size * 8) :
  318.                arg_type;
  319.             unsigned target_size = TD.getTypeStoreSize(target_type);
  320.             unsigned target_align = TD.getABITypeAlignment(target_type);
  321.  
  322.             if (llvm::isa<llvm::PointerType>(arg_type) && arg.hasByValAttr()) {
  323.                arg_type =
  324.                   llvm::dyn_cast<llvm::PointerType>(arg_type)->getElementType();
  325.             }
  326.  
  327.             if (arg_type->isPointerTy()) {
  328.                // XXX: Figure out LLVM->OpenCL address space mappings for each
  329.                // target.  I think we need to ask clang what these are.  For now,
  330.                // pretend everything is in the global address space.
  331.                unsigned address_space = llvm::cast<llvm::PointerType>(arg_type)->getAddressSpace();
  332.                switch (address_space) {
  333.                   default:
  334.                      args.push_back(
  335.                         module::argument(module::argument::global, arg_size,
  336.                                          target_size, target_align,
  337.                                          module::argument::zero_ext));
  338.                      break;
  339.                }
  340.  
  341.             } else {
  342.                llvm::AttributeSet attrs = kernel_func->getAttributes();
  343.                enum module::argument::ext_type ext_type =
  344.                   (attrs.hasAttribute(arg.getArgNo() + 1,
  345.                                      llvm::Attribute::SExt) ?
  346.                    module::argument::sign_ext :
  347.                    module::argument::zero_ext);
  348.  
  349.                args.push_back(
  350.                   module::argument(module::argument::scalar, arg_size,
  351.                                    target_size, target_align, ext_type));
  352.             }
  353.          }
  354.  
  355.          m.syms.push_back(module::symbol(kernel_name, 0, i, args ));
  356.       }
  357.  
  358.       header.num_bytes = llvm_bitcode.size();
  359.       std::string data;
  360.       data.insert(0, (char*)(&header), sizeof(header));
  361.       data.insert(data.end(), llvm_bitcode.begin(),
  362.                                   llvm_bitcode.end());
  363.       m.secs.push_back(module::section(0, module::section::text,
  364.                                        header.num_bytes, data));
  365.  
  366.       return m;
  367.    }
  368. } // End anonymous namespace
  369.  
  370. module
  371. clover::compile_program_llvm(const compat::string &source,
  372.                              enum pipe_shader_ir ir,
  373.                              const compat::string &target,
  374.                              const compat::string &opts) {
  375.  
  376.    std::vector<llvm::Function *> kernels;
  377.    size_t processor_str_len = std::string(target.begin()).find_first_of("-");
  378.    std::string processor(target.begin(), 0, processor_str_len);
  379.    std::string triple(target.begin(), processor_str_len + 1,
  380.                       target.size() - processor_str_len - 1);
  381.  
  382.    // The input file name must have the .cl extension in order for the
  383.    // CompilerInvocation class to recognize it as an OpenCL source file.
  384.    llvm::Module *mod = compile(source, "input.cl", triple, processor, opts);
  385.  
  386.    find_kernels(mod, kernels);
  387.  
  388.    link(mod, triple, processor, kernels);
  389.  
  390.    // Build the clover::module
  391.    switch (ir) {
  392.       case PIPE_SHADER_IR_TGSI:
  393.          //XXX: Handle TGSI
  394.          assert(0);
  395.          return module();
  396.       default:
  397.          return build_module_llvm(mod, kernels);
  398.    }
  399. }
  400.