Skip to content

Commit 524fdc1

Browse files
jakehehrlichcommit-bot@chromium.org
authored andcommitted
Reland "[llvm] Add initial scaffolding"
This is a reland of b71d2d9 Original change's description: > [llvm] Add initial scaffolding > > This change adds the gclient and GN changes needed to build > an executable using LLVM in the Dart tree as well as a basic > testing framework based on llvm-lit. > > Change-Id: I9009a98ff95043cc3754966f31697ba7f1712310 > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/106434 > Commit-Queue: Jake Ehrlich <jakehehrlich@google.com> > Reviewed-by: Alexander Thomas <athom@google.com> > Reviewed-by: Vyacheslav Egorov <vegorov@google.com> Change-Id: Ib3cd3299ed463133616c666285f9a58fa387b5bd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/107829 Reviewed-by: Vyacheslav Egorov <vegorov@google.com> Commit-Queue: Jake Ehrlich <jakehehrlich@google.com>
1 parent 45a9815 commit 524fdc1

File tree

19 files changed

+2069
-0
lines changed

19 files changed

+2069
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
/*.vcxproj.user
2424
*.stamp
2525

26+
# LLVM prebuilts
27+
/third_party/llvm/include
28+
/third_party/llvm/lib
29+
/third_party/llvm/.versions
30+
2631
# Gyp generated files
2732
*.xcodeproj
2833
*.intermediate
@@ -96,3 +101,4 @@ editor/util/testing/mac/Samples.suite/Results
96101
/outline.dill
97102
/generated/
98103
/crash_logs/
104+
/build/config/gclient_args.gni

BUILD.gn

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# for details. All rights reserved. Use of this source code is governed by a
33
# BSD-style license that can be found in the LICENSE file.
44

5+
import("build/config/gclient_args.gni")
56
import("build/dart/dart_host_sdk_toolchain.gni")
67

78
targetting_fuchsia = target_os == "fuchsia"
@@ -15,6 +16,11 @@ group("default") {
1516
deps = [
1617
":runtime",
1718
]
19+
if (checkout_llvm) {
20+
deps += [
21+
":llvm_codegen"
22+
]
23+
}
1824
}
1925

2026
group("most") {
@@ -116,6 +122,19 @@ group("analysis_server") {
116122
]
117123
}
118124

125+
group("check_llvm") {
126+
deps = [
127+
"runtime/llvm_codegen/test",
128+
]
129+
}
130+
131+
group("llvm_codegen") {
132+
deps = [
133+
"runtime/llvm_codegen/codegen",
134+
"runtime/llvm_codegen/bit",
135+
]
136+
}
137+
119138
# This is the target that is built on the dart2js build bots.
120139
# It must depend on anything that is required by the dart2js
121140
# test suites.

DEPS

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,20 @@ vars = {
146146
"crashpad_rev": "bf327d8ceb6a669607b0dbab5a83a275d03f99ed",
147147
"minichromium_rev": "8d641e30a8b12088649606b912c2bc4947419ccc",
148148
"googletest_rev": "f854f1d27488996dc8a6db3c9453f80b02585e12",
149+
150+
# An LLVM backend needs LLVM binaries and headers. To avoid build time
151+
# increases we can use prebuilts. We don't want to download this on every
152+
# CQ/CI bot nor do we want the average Dart developer to incur that cost.
153+
# So by default we will not download prebuilts.
154+
"checkout_llvm": False,
155+
"llvm_revision": "fe8bd96ebd6c490ea0b5c1fb342db2d7c393a109"
149156
}
150157

158+
gclient_gn_args_file = Var("dart_root") + '/build/config/gclient_args.gni'
159+
gclient_gn_args = [
160+
'checkout_llvm'
161+
]
162+
151163
deps = {
152164
# Stuff needed for GN build.
153165
Var("dart_root") + "/buildtools/clang_format/script":
@@ -404,6 +416,17 @@ deps = {
404416
],
405417
"dep_type": "cipd",
406418
},
419+
420+
Var("dart_root") + "/third_party/llvm": {
421+
"packages": [
422+
{
423+
"package": "fuchsia/lib/llvm/${{platform}}",
424+
"version": "git_revision:" + Var("llvm_revision"),
425+
},
426+
],
427+
"condition": "checkout_llvm",
428+
"dep_type": "cipd",
429+
},
407430
}
408431

409432
deps_os = {

runtime/bin/BUILD.gn

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# for details. All rights reserved. Use of this source code is governed by a
33
# BSD-style license that can be found in the LICENSE file.
44

5+
6+
import("../../build/config/gclient_args.gni")
57
import("../../build/dart/dart_action.gni")
68
import("../runtime_args.gni")
79
import("../vm/compiler/compiler_sources.gni")
@@ -923,6 +925,9 @@ executable("run_vm_tests") {
923925
"..:libdart_nosnapshot_with_precompiler",
924926
"//third_party/zlib",
925927
]
928+
if (checkout_llvm) {
929+
deps += [ "//runtime/llvm_codegen/bit:test" ]
930+
}
926931
include_dirs = [
927932
"..",
928933
"$target_gen_dir",

runtime/llvm_codegen/bit/BUILD.gn

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
# for details. All rights reserved. Use of this source code is governed by a
3+
# BSD-style license that can be found in the LICENSE file.
4+
5+
executable("bit") {
6+
sources = [
7+
"main.cc",
8+
]
9+
root_dir = rebase_path(root_out_dir)
10+
defines = [ "LIT_BINARY_DIR=\"$root_dir\"" ]
11+
deps = [
12+
"//third_party/llvm:LLVMSupport",
13+
]
14+
data_deps = [
15+
"//runtime/llvm_codegen/codegen",
16+
]
17+
}
18+
19+
source_set("test") {
20+
sources = [
21+
"test.cc",
22+
]
23+
24+
deps = [
25+
"//third_party/llvm:LLVMSupport",
26+
]
27+
28+
include_dirs = [ "//runtime" ]
29+
30+
defines = [ "TESTING" ]
31+
}

runtime/llvm_codegen/bit/bit.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
#include <stdio.h>
6+
7+
#include <map>
8+
#include <regex>
9+
10+
#include "llvm/ADT/StringRef.h"
11+
#include "llvm/Support/LineIterator.h"
12+
#include "llvm/Support/MemoryBuffer.h"
13+
#include "llvm/Support/Path.h"
14+
#include "llvm/Support/Regex.h"
15+
#include "llvm/Support/WithColor.h"
16+
17+
namespace {
18+
19+
using namespace llvm;
20+
21+
struct Config {
22+
StringRef filename;
23+
StringRef out_dir;
24+
};
25+
26+
StringMap<std::string> GetSubstitutions(const Config& config) {
27+
// Compute all of our strings needed for substitutions.
28+
StringRef test_dir = sys::path::parent_path(config.filename);
29+
StringRef basename = sys::path::filename(config.filename);
30+
SmallString<128> tmp_file;
31+
sys::path::append(tmp_file, sys::path::Style::native, config.out_dir,
32+
basename + ".tmp");
33+
SmallString<128> codegen;
34+
sys::path::append(codegen, sys::path::Style::native, LIT_BINARY_DIR,
35+
"codegen");
36+
SmallString<128> bit;
37+
sys::path::append(bit, sys::path::Style::native, LIT_BINARY_DIR, "bit");
38+
39+
// Set up our substitutions.
40+
StringMap<std::string> subs;
41+
subs["s"] = config.filename.str();
42+
subs["p"] = test_dir.str();
43+
subs["P"] = test_dir.str();
44+
subs["t"] = tmp_file.str().str();
45+
subs["codegen"] = codegen.str().str();
46+
subs["bit"] = bit.str().str();
47+
return subs;
48+
}
49+
50+
std::string PerformSubstitutions(const StringMap<std::string>& subs,
51+
StringRef string) {
52+
std::string out = string.str();
53+
for (const auto& sub : subs) {
54+
std::string key = (Twine("%") + sub.getKeyData()).str();
55+
size_t pos = 0;
56+
while ((pos = out.find(key, pos)) != std::string::npos) {
57+
if (pos != 0 && out[pos - 1] == '%') {
58+
pos += key.size();
59+
continue;
60+
}
61+
out.replace(pos, key.size(), sub.getValue());
62+
pos += sub.second.size();
63+
}
64+
}
65+
return out;
66+
}
67+
68+
Optional<std::string> GetCommand(StringRef line) {
69+
static Regex run_line("^;[ ]*RUN:[ ]*(.*)$");
70+
SmallVector<StringRef, 2> cmd;
71+
if (!run_line.match(line, &cmd)) return Optional<std::string>{};
72+
assert(cmd.size() == 2);
73+
return cmd[1].str();
74+
}
75+
76+
} // namespace

runtime/llvm_codegen/bit/main.cc

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
#include <stdio.h>
6+
7+
#include <cctype>
8+
#include <map>
9+
#include <regex>
10+
11+
#include "bit.h"
12+
#include "llvm/ADT/StringRef.h"
13+
#include "llvm/Support/InitLLVM.h"
14+
#include "llvm/Support/LineIterator.h"
15+
#include "llvm/Support/MemoryBuffer.h"
16+
#include "llvm/Support/Path.h"
17+
#include "llvm/Support/Process.h"
18+
#include "llvm/Support/Program.h"
19+
#include "llvm/Support/Regex.h"
20+
#include "llvm/Support/VirtualFileSystem.h"
21+
#include "llvm/Support/WithColor.h"
22+
23+
using namespace llvm;
24+
25+
namespace {
26+
27+
StringRef tool_name;
28+
29+
LLVM_ATTRIBUTE_NORETURN void Fail(Twine message) {
30+
WithColor::error(errs(), tool_name) << message << ".\n";
31+
errs().flush();
32+
exit(1);
33+
}
34+
35+
LLVM_ATTRIBUTE_NORETURN void Fail(Error e) {
36+
assert(E);
37+
std::string buf;
38+
raw_string_ostream os(buf);
39+
logAllUnhandledErrors(std::move(e), os);
40+
os.flush();
41+
WithColor::error(errs(), tool_name) << buf;
42+
exit(1);
43+
}
44+
45+
LLVM_ATTRIBUTE_NORETURN void ReportError(StringRef file, std::error_code ec) {
46+
assert(ec);
47+
Fail(createFileError(file, ec));
48+
}
49+
50+
std::string ReadFile(FILE* file) {
51+
std::string output;
52+
constexpr size_t buf_size = 256;
53+
char buf[buf_size];
54+
size_t size;
55+
while ((size = fread(buf, buf_size, sizeof(buf[0]), file)))
56+
output.append(buf, buf + buf_size);
57+
return output;
58+
}
59+
60+
bool IsPosixFullyPortablePath(StringRef path) {
61+
const char* extra = "._-/";
62+
for (auto c : path)
63+
if (!isalnum(c) && !strchr(extra, c)) return false;
64+
return true;
65+
}
66+
67+
} // namespace
68+
69+
int main(int argc, char** argv) {
70+
InitLLVM X(argc, argv);
71+
72+
// Make sure we have both arguments.
73+
tool_name = argv[0];
74+
if (argc != 3) Fail("expected exactly 2 arguments");
75+
76+
// Makes sure that stdin/stdout are setup correctly.
77+
if (sys::Process::FixupStandardFileDescriptors())
78+
Fail("std in/out fixup failed");
79+
80+
// Set our config.
81+
Config config;
82+
config.filename = argv[1];
83+
config.out_dir = argv[2];
84+
85+
// Make sure we have valid filepaths.
86+
if (!IsPosixFullyPortablePath(config.filename))
87+
Fail("'" + config.filename + "' is not a posix fully portable filename");
88+
if (!IsPosixFullyPortablePath(config.out_dir))
89+
Fail("'" + config.out_dir + "' is not a posix fully portable filename");
90+
91+
// Compute substitutions.
92+
auto subs = GetSubstitutions(config);
93+
94+
// The lines we execute are allowed to assume that %p will exist.
95+
sys::fs::create_directory(subs["p"]);
96+
97+
// Open the file for reading.
98+
auto buf_or = vfs::getRealFileSystem()->getBufferForFile(config.filename);
99+
if (!buf_or) ReportError(config.filename, buf_or.getError());
100+
auto buf = std::move(*buf_or);
101+
102+
// Now iterate over the lines in the file.
103+
line_iterator it{*buf};
104+
int count = 0;
105+
for (StringRef line = *it; !it.is_at_end(); line = *++it) {
106+
auto cmd = GetCommand(line);
107+
if (!cmd) continue;
108+
++count;
109+
auto subbed = PerformSubstitutions(subs, *cmd);
110+
FILE* file = popen(subbed.c_str(), "r");
111+
std::string output = ReadFile(file);
112+
if (pclose(file) != 0) {
113+
errs() << output << "\n";
114+
Fail("Failure on line " + Twine(it.line_number()) + "\n\t" + subbed + "");
115+
}
116+
}
117+
if (count == 0) {
118+
Fail("No commands to run");
119+
}
120+
outs() << "Commands run: " << count << "\n";
121+
return 0;
122+
}

0 commit comments

Comments
 (0)