Apply .clangformat to all repo source files

Change-Id: I7e79c6058f0303f9a98911e3b7dd2e8596079344


[ROCm/hip commit: 1ba06f63c4]
This commit is contained in:
Maneesh Gupta
2018-03-12 11:29:03 +05:30
parent 52506ef382
commit 4f42ee762d
293 ha cambiato i file con 43980 aggiunte e 45830 eliminazioni
+6 -7
Vedi File
@@ -11,8 +11,7 @@ using namespace clara;
using namespace hip_impl;
using namespace std;
int main(int argc, char** argv)
{
int main(int argc, char** argv) {
try {
bool help = false;
vector<string> inputs;
@@ -24,7 +23,8 @@ int main(int argc, char** argv)
if (!r) throw runtime_error{r.errorMessage()};
if (help) cout << cmd << endl;
if (help)
cout << cmd << endl;
else {
if (inputs.empty()) throw runtime_error{"No inputs specified."};
@@ -33,13 +33,12 @@ int main(int argc, char** argv)
auto tmp = tokenize_targets(targets);
if (tmp.empty()) {
tmp.assign(amdgpu_targets().cbegin(), amdgpu_targets().cend());
}
else validate_targets(tmp);
} else
validate_targets(tmp);
extract_code_objects(inputs, tmp);
}
}
catch (const exception& ex) {
} catch (const exception& ex) {
cerr << ex.what() << endl;
return EXIT_FAILURE;
+51 -76
Vedi File
@@ -13,92 +13,67 @@
#include <string>
#include <vector>
namespace hip_impl
{
inline
clara::Parser cmdline_parser(
bool& help,
std::vector<std::string>& inputs,
std::string& targets)
{
return
clara::Help{help} |
clara::Arg{inputs, "a" + fat_binary_extension() + " etc."}(
"fat binaries which contain the code objects to be extracted; "
"the binary format of the file(s) is documented at: "
"https://reviews.llvm.org/D13909; "
"the code object format is documented at: "
"https://www.llvm.org/docs/AMDGPUUsage.html#code-object.") |
clara::Opt{targets, "gfx803,gfx900 etc."}
["-t"]["--targets"](
"targets for which code objects are to be extracted from "
"the fat binary; must be included in the set of processors "
"with ROCm support from "
"https://www.llvm.org/docs/AMDGPUUsage.html#processors.");
}
namespace hip_impl {
inline clara::Parser cmdline_parser(bool& help, std::vector<std::string>& inputs,
std::string& targets) {
return clara::Help{help} |
clara::Arg{inputs, "a" + fat_binary_extension() + " etc."}(
"fat binaries which contain the code objects to be extracted; "
"the binary format of the file(s) is documented at: "
"https://reviews.llvm.org/D13909; "
"the code object format is documented at: "
"https://www.llvm.org/docs/AMDGPUUsage.html#code-object.") |
clara::Opt{targets, "gfx803,gfx900 etc."}["-t"]["--targets"](
"targets for which code objects are to be extracted from "
"the fat binary; must be included in the set of processors "
"with ROCm support from "
"https://www.llvm.org/docs/AMDGPUUsage.html#processors.");
}
inline
std::string make_code_object_file_name(
const std::string& input, const std::string& target)
{
assert(!input.empty() && !target.empty());
inline std::string make_code_object_file_name(const std::string& input, const std::string& target) {
assert(!input.empty() && !target.empty());
auto r = input.substr(0, input.find(fat_binary_extension()));
r += '_' + target + code_object_extension();
auto r = input.substr(0, input.find(fat_binary_extension()));
r += '_' + target + code_object_extension();
return r;
}
return r;
}
inline
void extract_code_objects(
const std::vector<std::string>& inputs,
const std::vector<std::string>& targets)
{
for (auto&& input : inputs) {
std::ifstream tmp{input};
std::vector<char> bundle{
std::istreambuf_iterator<char>{tmp},
std::istreambuf_iterator<char>{}};
inline void extract_code_objects(const std::vector<std::string>& inputs,
const std::vector<std::string>& targets) {
for (auto&& input : inputs) {
std::ifstream tmp{input};
std::vector<char> bundle{std::istreambuf_iterator<char>{tmp},
std::istreambuf_iterator<char>{}};
Bundled_code_header tmp1{bundle};
Bundled_code_header tmp1{bundle};
if (!valid(tmp1)) {
throw std::runtime_error{input + " is not a valid fat binary."};
if (!valid(tmp1)) {
throw std::runtime_error{input + " is not a valid fat binary."};
}
for (auto&& target : targets) {
const auto it = std::find_if(
bundles(tmp1).cbegin(), bundles(tmp1).cend(),
[&](const Bundled_code& x) { return x.triple.find(target) != std::string::npos; });
if (it == bundles(tmp1).cend()) {
std::cerr << "Warning: " << input << " does not contain code for the " << target
<< " target.";
continue;
}
for (auto&& target : targets) {
const auto it = std::find_if(
bundles(tmp1).cbegin(),
bundles(tmp1).cend(),
[&](const Bundled_code& x) {
return x.triple.find(target) != std::string::npos;
});
if (it == bundles(tmp1).cend()) {
std::cerr << "Warning: " << input
<< " does not contain code for the " << target
<< " target.";
continue;
}
std::ofstream out{make_code_object_file_name(input, target)};
std::copy_n(
it->blob.cbegin(),
it->blob.size(),
std::ostreambuf_iterator<char>{out});
}
std::ofstream out{make_code_object_file_name(input, target)};
std::copy_n(it->blob.cbegin(), it->blob.size(), std::ostreambuf_iterator<char>{out});
}
}
}
inline
void validate_inputs(const std::vector<std::string>& inputs)
{
const auto it = std::find_if_not(
inputs.cbegin(), inputs.cend(), file_exists);
inline void validate_inputs(const std::vector<std::string>& inputs) {
const auto it = std::find_if_not(inputs.cbegin(), inputs.cend(), file_exists);
if (it != inputs.cend()) {
throw std::runtime_error{
"Non existent file " + *it + " passed as input."};
}
if (it != inputs.cend()) {
throw std::runtime_error{"Non existent file " + *it + " passed as input."};
}
}
}
} // namespace hip_impl
File diff soppresso perché troppo grande Carica Diff
+56 -69
Vedi File
@@ -8,86 +8,73 @@
#include <unordered_set>
#include <vector>
namespace hip_impl
{
inline
const std::unordered_set<std::string>& amdgpu_targets()
{ // The evolving list lives at:
// https://www.llvm.org/docs/AMDGPUUsage.html#processors.
static const std::unordered_set<std::string> r{
"gfx701", "gfx801", "gfx802", "gfx803", "gfx900"};
namespace hip_impl {
inline const std::unordered_set<std::string>& amdgpu_targets() { // The evolving list lives at:
// https://www.llvm.org/docs/AMDGPUUsage.html#processors.
static const std::unordered_set<std::string> r{"gfx701", "gfx801", "gfx802", "gfx803",
"gfx900"};
return r;
return r;
}
inline const std::string& code_object_extension() {
static const std::string r{".ffa"};
return r;
}
inline const std::string& fat_binary_extension() {
static const std::string r{".adipose"};
return r;
}
inline bool file_exists(const std::string& path_to) {
return static_cast<bool>(std::ifstream{path_to});
}
inline std::vector<std::string> tokenize_targets(
const std::string&
x) { // TODO: move to regular expressions once we clarify the need to support
// ancient standard library implementations.
if (x.empty()) return {};
static constexpr const char valid_characters[] = "gfx0123456789,";
if (x.find_first_not_of(valid_characters) != std::string::npos) {
throw std::runtime_error{"Invalid target string: " + x};
}
inline
const std::string& code_object_extension()
{
static const std::string r{".ffa"};
std::vector<std::string> r;
return r;
}
auto it = x.cbegin();
do {
auto it1 = std::find(it, x.cend(), ',');
r.emplace_back(it, it1);
inline
const std::string& fat_binary_extension()
{
static const std::string r{".adipose"};
if (it1 == x.cend()) break;
return r;
}
it = ++it1;
} while (true);
inline
bool file_exists(const std::string& path_to)
{
return static_cast<bool>(std::ifstream{path_to});
}
return r;
}
inline
std::vector<std::string> tokenize_targets(const std::string& x)
{ // TODO: move to regular expressions once we clarify the need to support
// ancient standard library implementations.
if (x.empty()) return {};
inline void validate_targets(const std::vector<std::string>& x) {
assert(!x.empty());
static constexpr const char valid_characters[] = "gfx0123456789,";
for (auto&& t : x) {
static const std::string digits{"0123456789"};
static const std::string pre{"gfx"};
if (x.find_first_not_of(valid_characters) != std::string::npos) {
throw std::runtime_error{"Invalid target string: " + x};
if (t.find(pre) != 0 || t.find_first_not_of(digits, pre.size()) != std::string::npos) {
throw std::runtime_error{"Invalid target: " + t};
}
std::vector<std::string> r;
auto it = x.cbegin();
do {
auto it1 = std::find(it, x.cend(), ',');
r.emplace_back(it, it1);
if (it1 == x.cend()) break;
it = ++it1;
} while (true);
return r;
}
inline
void validate_targets(const std::vector<std::string>& x)
{
assert(!x.empty());
for (auto&& t : x) {
static const std::string digits{"0123456789"};
static const std::string pre{"gfx"};
if (t.find(pre) != 0 ||
t.find_first_not_of(digits, pre.size()) != std::string::npos) {
throw std::runtime_error{"Invalid target: " + t};
}
if (amdgpu_targets().find(t) == amdgpu_targets().cend()) {
std::cerr << "Warning: target " << t
<< " has not been validated yet; it may be invalid."
<< std::endl;
}
if (amdgpu_targets().find(t) == amdgpu_targets().cend()) {
std::cerr << "Warning: target " << t
<< " has not been validated yet; it may be invalid." << std::endl;
}
}
} // Namespace hip_impl.
}
} // Namespace hip_impl.
+9 -10
Vedi File
@@ -11,12 +11,10 @@ using namespace clara;
using namespace hip_impl;
using namespace std;
int main(int argc, char** argv)
{
int main(int argc, char** argv) {
try {
if (!hipcc_and_lpl_colocated()) {
throw runtime_error{
"The LPL executable and hipcc must be in the same directory."};
throw runtime_error{"The LPL executable and hipcc must be in the same directory."};
}
bool help = false;
@@ -31,22 +29,23 @@ int main(int argc, char** argv)
if (!r) throw runtime_error{r.errorMessage()};
if (help) cout << cmd << endl;
if (help)
cout << cmd << endl;
else {
if (sources.empty()) throw runtime_error{"No inputs specified."};
auto tmp = tokenize_targets(targets);
if (tmp.empty()) {
tmp.assign(amdgpu_targets().cbegin(), amdgpu_targets().cend());
}
else validate_targets(tmp);
} else
validate_targets(tmp);
if (output.empty()) for (auto&& x : tmp) output += x;
if (output.empty())
for (auto&& x : tmp) output += x;
generate_fat_binary(sources, tmp, flags, output);
}
}
catch (const exception& ex) {
} catch (const exception& ex) {
cerr << ex.what() << endl;
return EXIT_FAILURE;
+103 -144
Vedi File
@@ -18,164 +18,123 @@
#include <utility>
#include <vector>
namespace hip_impl
{
inline
const std::string& kernel_section()
{
static const std::string r{".kernel"};
namespace hip_impl {
inline const std::string& kernel_section() {
static const std::string r{".kernel"};
return r;
return r;
}
inline const std::string& path_to_self() {
static constexpr const char self[] = "/proc/self/exe";
static std::string r;
static std::once_flag f;
std::call_once(f, []() {
using N = decltype(readlink(self, &r.front(), r.size()));
constexpr decltype(r.size()) max_path_sz{PATH_MAX};
N read_cnt;
do {
r.resize(std::max(2 * r.size(), max_path_sz));
read_cnt = readlink(self, &r.front(), r.size());
} while (read_cnt == -1 && r.size() < r.max_size());
r.resize(std::max(read_cnt, N{0}));
});
return r;
}
inline const std::string& path_to_hipcc() {
assert(!path_to_self().empty());
static const auto r = path_to_self().substr(0, path_to_self().find_last_of('/')) += "/hipcc";
return r;
}
inline std::string make_hipcc_call(const std::vector<std::string>& sources,
const std::vector<std::string>& targets,
const std::string& flags, const std::string& hipcc_output) {
assert(!sources.empty() && !targets.empty() && !hipcc_output.empty());
std::string r{path_to_hipcc() + ' '};
for (auto&& x : sources) r += x + ' ';
r += "-o " + hipcc_output + ' ';
for (auto&& x : targets) r += "--amdgpu-target=" + x + ' ';
r += flags + " -fPIC -shared";
return r;
}
inline void copy_kernel_section_to_fat_binary(const std::string& tmp, const std::string& output) {
ELFIO::elfio reader;
if (!reader.load(tmp)) {
throw std::runtime_error{"The result of the compilation is inaccessible."};
}
inline
const std::string& path_to_self()
{
static constexpr const char self[] = "/proc/self/exe";
const auto it =
std::find_if(reader.sections.begin(), reader.sections.end(),
[](const ELFIO::section* x) { return x->get_name() == kernel_section(); });
static std::string r;
static std::once_flag f;
std::ofstream out{output + fat_binary_extension()};
std::call_once(f, []() {
using N = decltype(readlink(self, &r.front(), r.size()));
if (it == reader.sections.end()) {
std::cerr << "Warning: no kernels were generated; fat binary shall "
"be empty."
<< std::endl;
} else {
std::copy_n((*it)->get_data(), (*it)->get_size(), std::ostreambuf_iterator<char>{out});
}
}
constexpr decltype(r.size()) max_path_sz{PATH_MAX};
N read_cnt;
do {
r.resize(std::max(2 * r.size(), max_path_sz));
read_cnt = readlink(self, &r.front(), r.size());
} while (read_cnt == -1 && r.size() < r.max_size());
inline void generate_fat_binary(const std::vector<std::string>& sources,
const std::vector<std::string>& targets, const std::string& flags,
const std::string& output) {
static const auto d = [](const std::string* f) { remove(f->c_str()); };
r.resize(std::max(read_cnt, N{0}));
});
std::unique_ptr<const std::string, decltype(d)> tmp{&output, d};
return r;
redi::ipstream hipcc{make_hipcc_call(sources, targets, flags, *tmp), redi::pstream::pstderr};
if (!hipcc.is_open()) {
throw std::runtime_error{"Compiler invocation failed."};
}
inline
const std::string& path_to_hipcc()
{
assert(!path_to_self().empty());
std::string log;
while (std::getline(hipcc, log)) std::cout << log << '\n';
static const auto r = path_to_self().substr(
0, path_to_self().find_last_of('/')) += "/hipcc";
hipcc.close();
return r;
if (hipcc.rdbuf()->exited() && hipcc.rdbuf()->status() != EXIT_SUCCESS) {
throw std::runtime_error{"Compilation failed."};
}
inline
std::string make_hipcc_call(
const std::vector<std::string>& sources,
const std::vector<std::string>& targets,
const std::string& flags,
const std::string& hipcc_output)
{
assert(!sources.empty() && !targets.empty() && !hipcc_output.empty());
copy_kernel_section_to_fat_binary(*tmp, output);
}
std::string r{path_to_hipcc() + ' '};
inline bool hipcc_and_lpl_colocated() {
if (path_to_self().empty()) return false;
for (auto&& x : sources) r += x + ' ';
r += "-o " + hipcc_output + ' ';
for (auto&& x : targets) r += "--amdgpu-target=" + x + ' ';
r += flags + " -fPIC -shared";
return file_exists(path_to_hipcc());
}
return r;
}
inline
void copy_kernel_section_to_fat_binary(
const std::string& tmp, const std::string& output)
{
ELFIO::elfio reader;
if (!reader.load(tmp)) {
throw std::runtime_error{
"The result of the compilation is inaccessible."};
}
const auto it = std::find_if(
reader.sections.begin(),
reader.sections.end(),
[](const ELFIO::section* x) {
return x->get_name() == kernel_section();
});
std::ofstream out{output + fat_binary_extension()};
if (it == reader.sections.end()) {
std::cerr << "Warning: no kernels were generated; fat binary shall "
"be empty." << std::endl;
}
else {
std::copy_n(
(*it)->get_data(),
(*it)->get_size(),
std::ostreambuf_iterator<char>{out});
}
}
inline
void generate_fat_binary(
const std::vector<std::string>& sources,
const std::vector<std::string>& targets,
const std::string& flags,
const std::string& output)
{
static const auto d = [](const std::string* f) { remove(f->c_str()); };
std::unique_ptr<const std::string, decltype(d)> tmp{&output, d};
redi::ipstream hipcc{
make_hipcc_call(sources, targets, flags, *tmp),
redi::pstream::pstderr};
if (!hipcc.is_open()) {
throw std::runtime_error{"Compiler invocation failed."};
}
std::string log;
while (std::getline(hipcc, log)) std::cout << log << '\n';
hipcc.close();
if (hipcc.rdbuf()->exited() &&
hipcc.rdbuf()->status() != EXIT_SUCCESS) {
throw std::runtime_error{"Compilation failed."};
}
copy_kernel_section_to_fat_binary(*tmp, output);
}
inline
bool hipcc_and_lpl_colocated()
{
if (path_to_self().empty()) return false;
return file_exists(path_to_hipcc());
}
inline
clara::Parser cmdline_parser(
bool& help,
std::vector<std::string>& sources,
std::string& targets,
std::string& flags,
std::string& output)
{
return
clara::Opt{flags, "\"-v -DMACRO etc.\""}
["-f"]["--flags"](
"flags for compilation; must be valid for hipcc.") |
clara::Help{help} |
clara::Opt{output, "filename"}
["-o"]["--output"](
"name of fat-binary output file; the binary format of the "
"file is documented at: https://reviews.llvm.org/D13909.") |
clara::Arg{sources, "a.cpp b.cpp etc."}(
"inputs for compilation; must contain valid C++ code.") |
clara::Opt{targets, "gfx803,gfx900 etc."}
["-t"]["--targets"](
"targets for AMDGPU lowering; must be included in the set "
"of processors with ROCm support from "
"https://www.llvm.org/docs/AMDGPUUsage.html#processors.");
}
}
inline clara::Parser cmdline_parser(bool& help, std::vector<std::string>& sources,
std::string& targets, std::string& flags, std::string& output) {
return clara::Opt{flags, "\"-v -DMACRO etc.\""}["-f"]["--flags"](
"flags for compilation; must be valid for hipcc.") |
clara::Help{help} |
clara::Opt{output, "filename"}["-o"]["--output"](
"name of fat-binary output file; the binary format of the "
"file is documented at: https://reviews.llvm.org/D13909.") |
clara::Arg{sources,
"a.cpp b.cpp etc."}("inputs for compilation; must contain valid C++ code.") |
clara::Opt{targets, "gfx803,gfx900 etc."}["-t"]["--targets"](
"targets for AMDGPU lowering; must be included in the set "
"of processors with ROCm support from "
"https://www.llvm.org/docs/AMDGPUUsage.html#processors.");
}
} // namespace hip_impl
File diff soppresso perché troppo grande Carica Diff