Skip to content

Commit dce289f

Browse files
authored
Add options for cross compilation (#164)
* Add cross-compilation options triple and mcpu * Add sysroot option * Do not override target options with cuda device ones (nvptx)
1 parent e5b8b83 commit dce289f

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

tools/mlir-clang/Lib/clang-mlir.cc

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4773,6 +4773,20 @@ static bool parseMLIR(const char *Argv0, std::vector<std::string> filenames,
47734773
}
47744774
if (FOpenMP)
47754775
Argv.push_back("-fopenmp");
4776+
if (TargetTripleOpt != "") {
4777+
char *chars = (char *)malloc(TargetTripleOpt.length() + 1);
4778+
memcpy(chars, TargetTripleOpt.data(), TargetTripleOpt.length());
4779+
chars[TargetTripleOpt.length()] = 0;
4780+
Argv.push_back("-target");
4781+
Argv.push_back(chars);
4782+
}
4783+
if (McpuOpt != "") {
4784+
auto a = "-mcpu=" + McpuOpt;
4785+
char *chars = (char *)malloc(a.length() + 1);
4786+
memcpy(chars, a.data(), a.length());
4787+
chars[a.length()] = 0;
4788+
Argv.push_back(chars);
4789+
}
47764790
if (Standard != "") {
47774791
auto a = "-std=" + Standard;
47784792
char *chars = (char *)malloc(a.length() + 1);
@@ -4787,6 +4801,13 @@ static bool parseMLIR(const char *Argv0, std::vector<std::string> filenames,
47874801
chars[ResourceDir.length()] = 0;
47884802
Argv.push_back(chars);
47894803
}
4804+
if (SysRoot != "") {
4805+
Argv.push_back("--sysroot");
4806+
char *chars = (char *)malloc(SysRoot.length() + 1);
4807+
memcpy(chars, SysRoot.data(), SysRoot.length());
4808+
chars[SysRoot.length()] = 0;
4809+
Argv.push_back(chars);
4810+
}
47904811
if (Verbose) {
47914812
Argv.push_back("-v");
47924813
}
@@ -4904,14 +4925,19 @@ static bool parseMLIR(const char *Argv0, std::vector<std::string> filenames,
49044925
Clang->getTarget().adjustTargetOptions(Clang->getCodeGenOpts(),
49054926
Clang->getTargetOpts());
49064927

4907-
module.get()->setAttr(
4908-
LLVM::LLVMDialect::getDataLayoutAttrName(),
4909-
StringAttr::get(module->getContext(),
4910-
Clang->getTarget().getDataLayoutString()));
4911-
module.get()->setAttr(
4912-
LLVM::LLVMDialect::getTargetTripleAttrName(),
4913-
StringAttr::get(module->getContext(),
4914-
Clang->getTarget().getTriple().getTriple()));
4928+
llvm::Triple jobTriple = Clang->getTarget().getTriple();
4929+
if (triple.str() == "" || !jobTriple.isNVPTX()) {
4930+
triple = jobTriple;
4931+
module.get()->setAttr(
4932+
LLVM::LLVMDialect::getTargetTripleAttrName(),
4933+
StringAttr::get(module->getContext(),
4934+
Clang->getTarget().getTriple().getTriple()));
4935+
DL = llvm::DataLayout(Clang->getTarget().getDataLayoutString());
4936+
module.get()->setAttr(
4937+
LLVM::LLVMDialect::getDataLayoutAttrName(),
4938+
StringAttr::get(module->getContext(),
4939+
Clang->getTarget().getDataLayoutString()));
4940+
}
49154941

49164942
for (const auto &FIF : Clang->getFrontendOpts().Inputs) {
49174943
// Reset the ID tables if we are reusing the SourceManager and parsing
@@ -4930,8 +4956,6 @@ static bool parseMLIR(const char *Argv0, std::vector<std::string> filenames,
49304956
Act.EndSourceFile();
49314957
}
49324958
}
4933-
DL = llvm::DataLayout(Clang->getTarget().getDataLayoutString());
4934-
triple = Clang->getTarget().getTriple();
49354959
}
49364960
return true;
49374961
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: mlir-clang --target aarch64-unknown-linux-gnu %s %stdinclude -S -o - | FileCheck %s -check-prefix=MLIR
2+
// RUN: mlir-clang --target aarch64-unknown-linux-gnu %s %stdinclude -emit-llvm -S -o - | FileCheck %s -check-prefix=LLVM
3+
4+
// MLIR: llvm.target_triple = "aarch64-unknown-linux-gnu"
5+
// LLVM: target triple = "aarch64-unknown-linux-gnu"
6+
7+
int main() { return 0; }

tools/mlir-clang/mlir-clang.cc

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ static cl::opt<std::string> MArch("march", cl::init(""),
117117
static cl::opt<std::string> ResourceDir("resource-dir", cl::init(""),
118118
cl::desc("Resource-dir"));
119119

120+
static cl::opt<std::string> SysRoot("sysroot", cl::init(""),
121+
cl::desc("sysroot"));
122+
120123
static cl::opt<bool> EarlyVerifier("early-verifier", cl::init(false),
121124
cl::desc("Enable verifier ASAP"));
122125

@@ -136,6 +139,13 @@ static cl::list<std::string> defines("D", cl::desc("defines"),
136139
static cl::list<std::string> Includes("include", cl::desc("includes"),
137140
cl::cat(toolOptions));
138141

142+
static cl::opt<std::string> TargetTripleOpt("target", cl::init(""),
143+
cl::desc("Target triple"),
144+
cl::cat(toolOptions));
145+
146+
static cl::opt<std::string>
147+
McpuOpt("mcpu", cl::init(""), cl::desc("Target CPU"), cl::cat(toolOptions));
148+
139149
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
140150

141151
class MemRefInsider
@@ -210,9 +220,14 @@ int emitBinary(char *Argv0, const char *filename,
210220

211221
DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagBuffer);
212222

223+
string TargetTriple;
224+
if (TargetTripleOpt == "")
225+
TargetTriple = llvm::sys::getDefaultTargetTriple();
226+
else
227+
TargetTriple = TargetTripleOpt;
228+
213229
const char *binary = Argv0;
214-
const unique_ptr<Driver> driver(
215-
new Driver(binary, llvm::sys::getDefaultTargetTriple(), Diags));
230+
const unique_ptr<Driver> driver(new Driver(binary, TargetTriple, Diags));
216231
driver->CC1Main = &ExecuteCC1Tool;
217232
std::vector<const char *> Argv;
218233
Argv.push_back(Argv0);
@@ -272,6 +287,8 @@ int emitBinary(char *Argv0, const char *filename,
272287

273288
if (ResourceDir != "")
274289
driver->ResourceDir = ResourceDir;
290+
if (SysRoot != "")
291+
driver->SysRoot = SysRoot;
275292
SmallVector<std::pair<int, const Command *>, 4> FailingCommands;
276293
int Res = 0;
277294

0 commit comments

Comments
 (0)