Skip to content

Commit

Permalink
Merge pull request #205 from RossBrunton/tip_fix
Browse files Browse the repository at this point in the history
[LLVM] Multi_llvm support for current tip changes
  • Loading branch information
RossBrunton authored Nov 14, 2023
2 parents a0a771d + f5204f9 commit eef1eb2
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ static llvm::TargetMachine *createTargetMachine(const {{cookiecutter.target_name
return llvm_target->createTargetMachine(
target.llvm_triple, target.llvm_cpu, target.llvm_features, options,
llvm::Reloc::Model::Static, llvm::CodeModel::Small,
llvm::CodeGenOpt::Aggressive);
multi_llvm::CodeGenOptLevel::Aggressive);
}

llvm::TargetMachine *{{cookiecutter.target_name.capitalize()}}Module::getTargetMachine() {
Expand Down
46 changes: 46 additions & 0 deletions modules/compiler/multi_llvm/include/multi_llvm/enums.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (C) Codeplay Software Limited
//
// Licensed under the Apache License, Version 2.0 (the "License") with LLVM
// Exceptions; you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://github.com/codeplaysoftware/oneapi-construction-kit/blob/main/LICENSE.txt
//
// 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.
//
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef MULTI_LLVM_ENUMS_H_INCLUDED
#define MULTI_LLVM_ENUMS_H_INCLUDED

#include <llvm/Support/CodeGen.h>
#include <multi_llvm/llvm_version.h>

namespace multi_llvm {
#if LLVM_VERSION_MAJOR >= 18

typedef llvm::CodeGenFileType CodeGenFileType;
typedef llvm::CodeGenOptLevel CodeGenOptLevel;

#else

struct CodeGenFileType {
static constexpr auto AssemblyFile = llvm::CGFT_AssemblyFile;
static constexpr auto ObjectFile = llvm::CGFT_ObjectFile;
static constexpr auto Null = llvm::CGFT_Null;
};

struct CodeGenOptLevel {
static constexpr auto None = llvm::CodeGenOpt::None;
static constexpr auto Less = llvm::CodeGenOpt::Less;
static constexpr auto Default = llvm::CodeGenOpt::Default;
static constexpr auto Aggressive = llvm::CodeGenOpt::Aggressive;
};

#endif
} // namespace multi_llvm

#endif // MULTI_LLVM_ENUMS_H_INCLUDED
37 changes: 37 additions & 0 deletions modules/compiler/multi_llvm/include/multi_llvm/loop_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (C) Codeplay Software Limited
//
// Licensed under the Apache License, Version 2.0 (the "License") with LLVM
// Exceptions; you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://github.com/codeplaysoftware/oneapi-construction-kit/blob/main/LICENSE.txt
//
// 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.
//
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef MULTI_LLVM_LOOP_UTILS_H_INCLUDED
#define MULTI_LLVM_LOOP_UTILS_H_INCLUDED

#include <llvm/Transforms/Utils/LoopUtils.h>
#include <multi_llvm/llvm_version.h>

namespace multi_llvm {

inline llvm::Value *createSimpleTargetReduction(
llvm::IRBuilderBase &B, const llvm::TargetTransformInfo *TTI,
llvm::Value *Src, llvm::RecurKind RdxKind) {
#if LLVM_VERSION_MAJOR >= 18
(void)TTI;
return llvm::createSimpleTargetReduction(B, Src, RdxKind);
#else
return llvm::createSimpleTargetReduction(B, TTI, Src, RdxKind);
#endif
}

} // namespace multi_llvm

#endif // MULTI_LLVM_LOOP_UTILS_H_INCLUDED
2 changes: 2 additions & 0 deletions modules/compiler/multi_llvm/include/multi_llvm/multi_llvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
#ifndef MULTI_LLVM_MULTI_LLVM_H_INCLUDED
#define MULTI_LLVM_MULTI_LLVM_H_INCLUDED

#include <multi_llvm/enums.h>
#include <multi_llvm/llvm_version.h>
#include <multi_llvm/loop_utils.h>
#include <multi_llvm/triple.h>

#endif // MULTI_LLVM_MULTI_LLVM_H_INCLUDED
2 changes: 1 addition & 1 deletion modules/compiler/riscv/source/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ static llvm::TargetMachine *createTargetMachine(
return llvm_target->createTargetMachine(
target.llvm_triple, target.llvm_cpu, target.llvm_features, options,
llvm::Reloc::Model::Static, llvm::CodeModel::Small,
llvm::CodeGenOpt::Aggressive);
multi_llvm::CodeGenOptLevel::Aggressive);
}

llvm::TargetMachine *riscv::RiscvModule::getTargetMachine() {
Expand Down
2 changes: 1 addition & 1 deletion modules/compiler/source/base/source/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,7 @@ clang::FrontendInputFile BaseModule::prepareOpenCLInputFile(

auto addIncludeFile = [&](const std::string &name, const void *data,
const size_t size) {
const auto *entry = instance.getFileManager().getVirtualFile(
clang::FileEntryRef entry = instance.getFileManager().getVirtualFileRef(
"include" PATH_SEPARATOR + name, size, 0);
std::unique_ptr<llvm::MemoryBuffer> buffer{
new BakedMemoryBuffer(data, size)};
Expand Down
6 changes: 5 additions & 1 deletion modules/compiler/source/base/source/pass_pipelines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@
#include <llvm/ADT/StringSwitch.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/Passes/PassBuilder.h>
#include <llvm/Support/CodeGen.h>
#include <llvm/Target/TargetMachine.h>
#include <llvm/Transforms/IPO/GlobalOpt.h>
#include <llvm/Transforms/IPO/Inliner.h>
#include <llvm/Transforms/IPO/Internalize.h>
#include <multi_llvm/multi_llvm.h>

#include <optional>

Expand Down Expand Up @@ -128,7 +130,9 @@ void addLLVMDefaultPerModulePipeline(ModulePassManager &PM, PassBuilder &PB,
Result emitCodeGenFile(llvm::Module &M, TargetMachine *TM,
raw_pwrite_stream &ostream, bool create_assembly) {
legacy::PassManager PM;
CodeGenFileType type = !create_assembly ? CGFT_ObjectFile : CGFT_AssemblyFile;
CodeGenFileType type = !create_assembly
? multi_llvm::CodeGenFileType::ObjectFile
: multi_llvm::CodeGenFileType::AssemblyFile;
if (TM->addPassesToEmitFile(PM, ostream, /*DwoOut*/ nullptr, type,
/*DisableVerify*/ false)) {
return compiler::Result::FAILURE;
Expand Down
3 changes: 2 additions & 1 deletion modules/compiler/targets/host/source/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <llvm/Support/Host.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Target/TargetMachine.h>
#include <multi_llvm/multi_llvm.h>

#include "host/device.h"
#include "host/info.h"
Expand Down Expand Up @@ -193,7 +194,7 @@ compiler::Result HostTarget::initWithBuiltins(

llvm::orc::JITTargetMachineBuilder TMBuilder(triple);
TMBuilder.setCPU(CPUName.str());
TMBuilder.setCodeGenOptLevel(llvm::CodeGenOpt::Aggressive);
TMBuilder.setCodeGenOptLevel(multi_llvm::CodeGenOptLevel::Aggressive);
for (auto &Feature : FeatureMap) {
TMBuilder.getFeatures().AddFeature(Feature.first(), Feature.second);
}
Expand Down
19 changes: 11 additions & 8 deletions modules/compiler/tools/muxc/muxc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <llvm/Support/Error.h>
#include <llvm/Support/FormatVariadic.h>
#include <llvm/Support/ToolOutputFile.h>
#include <multi_llvm/llvm_version.h>
#include <multi_llvm/multi_llvm.h>

using namespace llvm;

Expand Down Expand Up @@ -55,10 +55,13 @@ static cl::opt<bool> WriteTextual(
cl::init(true));

static cl::opt<CodeGenFileType> FileType(
"filetype", cl::init(CGFT_AssemblyFile), cl::desc("Choose a file type:"),
cl::values(clEnumValN(CGFT_AssemblyFile, "asm", "Emit a textual file"),
clEnumValN(CGFT_ObjectFile, "obj", "Emit a binary object file"),
clEnumValN(CGFT_Null, "null",
"filetype", cl::init(multi_llvm::CodeGenFileType::AssemblyFile),
cl::desc("Choose a file type:"),
cl::values(clEnumValN(multi_llvm::CodeGenFileType::AssemblyFile, "asm",
"Emit a textual file"),
clEnumValN(multi_llvm::CodeGenFileType::ObjectFile, "obj",
"Emit a binary object file"),
clEnumValN(multi_llvm::CodeGenFileType::Null, "null",
"Emit nothing, for performance testing")));

static cl::opt<int> DeviceIdx(
Expand Down Expand Up @@ -138,14 +141,14 @@ int main(int argc, char **argv) {
}
}

if (FileType == CGFT_Null) {
if (FileType == multi_llvm::CodeGenFileType::Null) {
return 0;
}

// Open the output file.
std::error_code EC;
sys::fs::OpenFlags OpenFlags = sys::fs::OF_None;
if (FileType == CGFT_AssemblyFile) {
if (FileType == multi_llvm::CodeGenFileType::AssemblyFile) {
OpenFlags |= sys::fs::OF_Text;
}
auto Out = std::make_unique<ToolOutputFile>(OutputFilename, EC, OpenFlags);
Expand All @@ -154,7 +157,7 @@ int main(int argc, char **argv) {
return 1;
}

if (FileType == CGFT_AssemblyFile) {
if (FileType == multi_llvm::CodeGenFileType::AssemblyFile) {
Out->os() << *M.get();
} else {
WriteBitcodeToFile(*M.get(), Out->os());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ Value *createMaybeVPTargetReduction(IRBuilderBase &B,
assert(isa<VectorType>(Val->getType()) && "Must be vector type");
// If VL is null, it's not a vector-predicated reduction.
if (!VL) {
return createSimpleTargetReduction(B, &TTI, Val, Kind);
return multi_llvm::createSimpleTargetReduction(B, &TTI, Val, Kind);
}
auto IntrinsicOp = Intrinsic::not_intrinsic;
switch (Kind) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ unsigned calculateBoolReductionCost(LLVMContext &context, Module *module,
auto *F = Function::Create(new_fty, Function::InternalLinkage, "tmp", module);
auto *BB = BasicBlock::Create(context, "reduce", F);
IRBuilder<> B(BB);
createSimpleTargetReduction(B, &TTI, &*F->arg_begin(), RecurKind::And);
multi_llvm::createSimpleTargetReduction(B, &TTI, &*F->arg_begin(),
RecurKind::And);
unsigned cost = calculateBlockCost(*BB, TTI);

// We don't really need that function in the module anymore because it's
Expand Down

0 comments on commit eef1eb2

Please sign in to comment.