-
Notifications
You must be signed in to change notification settings - Fork 744
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
164 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,3 +125,4 @@ case $PLATFORM in | |
esac | ||
|
||
cd ../.. | ||
cp ../../src/cpp/*.h include/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* Copyright (C) 2020 Yu Kobayashi | ||
* | ||
* Licensed either under the Apache License, Version 2.0, or (at your option) | ||
* under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation (subject to the "Classpath" exception), | ||
* either version 2, or any later version (collectively, the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* http://www.gnu.org/licenses/ | ||
* http://www.gnu.org/software/classpath/license.html | ||
* | ||
* or as provided in the LICENSE.txt file that accompanied this code. | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef JAVACPP_LLVM_HELPER_H | ||
#define JAVACPP_LLVM_HELPER_H | ||
|
||
#include "llvm/ExecutionEngine/ExecutionEngine.h" | ||
#include "llvm/Target/TargetMachine.h" | ||
#include "llvm/Transforms/IPO.h" | ||
#include "llvm/Transforms/IPO/PassManagerBuilder.h" | ||
#include "llvm/IR/Verifier.h" | ||
#include "llvm/IR/LegacyPassManager.h" | ||
#include "llvm/CodeGen/TargetPassConfig.h" | ||
#include "llvm/Support/TargetRegistry.h" | ||
#include "llvm/Analysis/TargetLibraryInfo.h" | ||
#include "llvm/Analysis/TargetTransformInfo.h" | ||
#include "llvm/MC/SubtargetFeature.h" | ||
#include "llvm/Pass.h" | ||
#include "llvm-c/Transforms/PassManagerBuilder.h" | ||
#include "llvm-c/Types.h" | ||
|
||
using namespace llvm; | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
static void JavaCPPLLVMAddOptimizationPasses( | ||
legacy::PassManagerBase &passes, | ||
legacy::FunctionPassManager &fnPasses, | ||
TargetMachine *machine, | ||
unsigned optLevel, | ||
unsigned sizeLevel | ||
) { | ||
PassManagerBuilder builder; | ||
builder.OptLevel = optLevel; | ||
builder.SizeLevel = sizeLevel; | ||
builder.Inliner = createFunctionInliningPass(optLevel, sizeLevel, false); | ||
builder.LoopVectorize = optLevel > 1 && sizeLevel < 2; | ||
builder.SLPVectorize = optLevel > 1 && sizeLevel < 2; | ||
machine->adjustPassManager(builder); | ||
|
||
builder.populateFunctionPassManager(fnPasses); | ||
builder.populateModulePassManager(passes); | ||
} | ||
|
||
static void JavaCPPLLVMAddStandardLinkPasses(legacy::PassManagerBase &passes, unsigned optLevel, unsigned sizeLevel) { | ||
PassManagerBuilder builder; | ||
builder.VerifyInput = true; | ||
builder.Inliner = createFunctionInliningPass(optLevel, sizeLevel, false); | ||
builder.populateLTOPassManager(passes); | ||
} | ||
|
||
LLVMBool JavaCPPLLVMOptimizeModule( | ||
LLVMModuleRef moduleRef, | ||
unsigned optLevel, | ||
unsigned sizeLevel, | ||
char** outError | ||
) { | ||
// This function is based on main() of llvm/tools/opt/opt.cpp | ||
Module *module = unwrap(moduleRef); | ||
|
||
std::string error; | ||
std::string cpuStr = sys::getHostCPUName(); | ||
|
||
EngineBuilder engineBuilder; | ||
TargetMachine *machine = engineBuilder | ||
.setEngineKind(EngineKind::JIT) | ||
.setMCPU(cpuStr) | ||
.setErrorStr(&error) | ||
.setOptLevel(static_cast<CodeGenOpt::Level>(optLevel)) | ||
.selectTarget(); | ||
if (machine == nullptr) { | ||
*outError = strdup(error.c_str()); | ||
return 1; | ||
} | ||
|
||
module->setTargetTriple(machine->getTargetTriple().str()); | ||
module->setDataLayout(machine->createDataLayout()); | ||
|
||
legacy::PassManager passes; | ||
passes.add(new TargetLibraryInfoWrapperPass(machine->getTargetTriple())); | ||
passes.add(createTargetTransformInfoWrapperPass(machine->getTargetIRAnalysis())); | ||
|
||
legacy::FunctionPassManager fnPasses(module); | ||
fnPasses.add(createTargetTransformInfoWrapperPass(machine->getTargetIRAnalysis())); | ||
|
||
JavaCPPLLVMAddOptimizationPasses(passes, fnPasses, machine, optLevel, sizeLevel); | ||
JavaCPPLLVMAddStandardLinkPasses(passes, optLevel, sizeLevel); | ||
|
||
fnPasses.doInitialization(); | ||
for (Function &func : *module) { | ||
fnPasses.run(func); | ||
} | ||
fnPasses.doFinalization(); | ||
|
||
passes.add(createVerifierPass()); | ||
passes.run(*module); | ||
|
||
return 0; | ||
} | ||
|
||
LLVMBool JavaCPPLLVMCreateOptimizedJITCompilerForModule( | ||
LLVMExecutionEngineRef *outJIT, | ||
LLVMModuleRef moduleRef, | ||
unsigned optLevel, | ||
char **outError | ||
) { | ||
std::string error; | ||
std::string cpuStr = sys::getHostCPUName(); | ||
|
||
EngineBuilder engineBuilder(std::unique_ptr<Module>(unwrap(moduleRef))); | ||
ExecutionEngine *ee = engineBuilder | ||
.setEngineKind(EngineKind::JIT) | ||
.setMCPU(cpuStr) | ||
.setErrorStr(&error) | ||
.setOptLevel(static_cast<CodeGenOpt::Level>(optLevel)) | ||
.create(); | ||
if (ee == nullptr) { | ||
*outError = strdup(error.c_str()); | ||
return 1; | ||
} | ||
ee->finalizeObject(); | ||
*outJIT = wrap(ee); | ||
return 0; | ||
} | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters