Ignore:
Timestamp:
Jul 18, 2015, 10:18:34 AM (4 years ago)
Author:
nmedfort
Message:

First attempt to intergrate 'generate_predefined_ucd_functions' into build process.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/generate_predefined_ucd_functions.cpp

    r4680 r4684  
    3535#include "llvm/Support/FileSystem.h"
    3636#include <llvm/Transforms/Scalar.h>
    37 #include <boost/algorithm/string/case_conv.hpp>
    3837#include <iostream>
    3938
     
    4342using namespace llvm;
    4443
    45 inline std::string lowercase(const std::string & name) {
    46     std::locale loc;
    47     return boost::algorithm::to_lower_copy(name, loc);
    48 }
    49 
    5044static cl::opt<std::string>
    51 ObjectFilename("o", cl::desc("Output Object filename"), cl::value_desc("filename"));
     45ObjectFilename("o", cl::desc("Output object filename"), cl::value_desc("filename"), cl::Required);
    5246
    5347static cl::opt<std::string>
    54 PropertyFilename("p", cl::desc("Install Property filename"), cl::value_desc("filename"));
     48UCDSourcePath("dir", cl::desc("UCD source code directory"), cl::value_desc("directory"), cl::Required);
     49
     50static cl::opt<std::string>
     51UCDLibraryPath("lib", cl::desc("Static UCD library path"), cl::value_desc("filename"), cl::Required);
    5552
    5653#ifdef ENABLE_MULTIPLEXING
     
    7168    PabloBuilder builder(function.getEntryBlock());
    7269    // Build the unicode set function
    73     ucdCompiler.generateWithDefaultIfHierarchy(set, builder);
     70    PabloAST * target = ucdCompiler.generateWithDefaultIfHierarchy(set, builder);
     71    function.setResult(0, builder.createAssign("matches", target));
    7472    // Optimize it at the pablo level
    7573    Simplifier::optimize(function);
     
    9189 ** ------------------------------------------------------------------------------------------------------------- */
    9290
    93 void writePropertyInstaller(property_list && properties) {
    94 
     91void writePrecompiledProperties(property_list && properties) {
     92
     93    const std::string headerFilename = UCDSourcePath + "/precompiled_properties.h";
    9594    #ifdef USE_LLVM_3_5
    9695    std::string error;
    97     raw_fd_ostream out(PropertyFilename.c_str(), error, sys::fs::F_None);
     96    raw_fd_ostream header(headerFilename.c_str(), error, sys::fs::F_None);
    9897    if (!error.empty()) {
    9998        throw std::runtime_error(error);
     
    101100    #else
    102101    std::error_code error;
    103     raw_fd_ostream out(PropertyFilename, error, sys::fs::F_None);
     102    raw_fd_ostream header(headerFilename, error, sys::fs::F_None);
    104103    if (error) {
    105104        throw std::runtime_error(error.message());
     
    107106    #endif
    108107
    109     out << "#ifndef PROPERTYINSTALL\n";
    110     out << "#define PROPERTYINSTALL\n\n";
    111     out << "#include <include/simd-lib/bitblock.hpp>\n";
    112     out << "#include <pablo/pablo_compiler.h>\n\n";
    113     out << "namespace UCD {\n\n";
    114     out << "struct Input {\n    BitBlock bit[8];\n};\n\n";
    115     out << "struct Output {\n    BitBlock bit[1];\n};\n\n";
     108    header << "#ifndef PRECOMPILED_PROPERTIES\n";
     109    header << "#define PRECOMPILED_PROPERTIES\n\n";
     110    header << "#include <string>\n\n";
     111    header << "#include <tuple>\n";
     112    header << "namespace UCD {\n\n";
     113    header << "using ExternalProperty = std::tuple<void *, unsigned, unsigned, size_t>;\n\n";
     114    header << "const ExternalProperty & resolveExternalProperty(const std::string & name);\n\n";
     115    header << "}\n\n";
     116    header << "#endif\n";
     117    header.close();
     118
     119    const std::string cppFilename = UCDSourcePath + "/precompiled_properties.cpp";
     120    #ifdef USE_LLVM_3_5
     121    raw_fd_ostream cpp(cppFilename.c_str(), error, sys::fs::F_None);
     122    if (!error.empty()) {
     123        throw std::runtime_error(error);
     124    }
     125    #else
     126    raw_fd_ostream cpp(cppFilename, error, sys::fs::F_None);
     127    if (error) {
     128        throw std::runtime_error(error.message());
     129    }
     130    #endif
     131
     132    cpp << "#include \"precompiled_properties.h\"\n";
     133    cpp << "#include <include/simd-lib/bitblock.hpp>\n";
     134    cpp << "#include <stdexcept>\n";
     135    cpp << "#include <unordered_map>\n\n";
     136    cpp << "namespace UCD {\n\n";
     137    cpp << "struct Input {\n    BitBlock bit[8];\n};\n\n";
     138    cpp << "struct Output {\n    BitBlock bit[1];\n};\n\n";
    116139    for (auto prop : properties) {
    117         out << "extern \"C\" void " + prop.first + "(const Input &, BitBlock *, Output &);\n";
    118     }
    119     out << "\nvoid install_properties(pablo::PabloCompiler & p) {\n";
    120     for (auto prop : properties) {
    121         out << "    p.InstallExternalFunction(\"" + prop.first + "\", reinterpret_cast<void *>(&" + prop.first + "), " + std::to_string(prop.second) + ");\n";
    122     }
    123     out << "}\n}\n\n#endif\n";
    124     out.close();
     140        cpp << "extern \"C\" void " + prop.first + "(const Input &, BitBlock *, Output &);\n";
     141    }
     142
     143    cpp << "\nconst static std::unordered_map<std::string, ExternalProperty> ExternalPropertyMap = {\n";
     144    for (auto itr = properties.begin(); itr != properties.end(); ) {
     145        cpp << "    {\"" + itr->first + "\", std::make_tuple(reinterpret_cast<void *>(&" + itr->first + "), 8, 1, " + std::to_string(itr->second) + ")}";
     146        if (++itr != properties.end()) {
     147            cpp << ",";
     148        }
     149        cpp << "\n";
     150    }
     151    cpp << "};\n\n";
     152
     153    cpp << "const ExternalProperty & resolveExternalProperty(const std::string & name) {\n";
     154    cpp << "    auto f = ExternalPropertyMap.find(name);\n";
     155    cpp << "    if (f == ExternalPropertyMap.end())\n";
     156    cpp << "        throw std::runtime_error(\"No external property named \\\"\" + name + \"\\\" found!\");\n";
     157    cpp << "    return f->second;\n";
     158    cpp << "}\n\n}\n";
     159
     160    cpp.close();
     161
    125162}
    126163
     
    160197    verifyModule(*module, &dbgs());
    161198
    162     writePropertyInstaller(std::move(properties));
     199    writePrecompiledProperties(std::move(properties));
    163200
    164201    return module;
     
    252289int main(int argc, char *argv[]) {
    253290    cl::ParseCommandLineOptions(argc, argv, "UCD Compiler\n");
    254     if (PropertyFilename.empty()) {
    255         PropertyFilename = "PropertyInstall.h";
    256     }
    257     if (ObjectFilename.empty()) {
    258         ObjectFilename = "pregenerated_properties.o";
    259     }
    260291    Module * module = generateUCDModule();
    261292    compileUCDModule(module);
Note: See TracChangeset for help on using the changeset viewer.