Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[build] [bug] Fix building on macOS 10.14 failed #5332

Merged
merged 2 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 10 additions & 28 deletions taichi/runtime/llvm/llvm_offline_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@

#include <queue>

#if __has_include(<filesystem>)
#include <filesystem>
namespace fs = std::filesystem;
#elif __has_include(<experimental/filesystem>)
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
error "Missing the <filesystem> header."
#endif // __has_include(<filesystem>)

#include "llvm/AsmParser/Parser.h"
#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/Bitcode/BitcodeWriter.h"
Expand Down Expand Up @@ -39,16 +29,12 @@ static bool is_current_llvm_cache_version(
}

static std::string get_llvm_cache_metadata_file_path(const std::string &dir) {
std::stringstream tcb_ss;
tcb_ss << dir << "/" << kMetadataFilename << ".tcb";
return tcb_ss.str();
return taichi::join_path(dir, std::string(kMetadataFilename) + ".tcb");
}

static std::string get_llvm_cache_metadata_json_file_path(
const std::string &dir) {
std::stringstream tcb_ss;
tcb_ss << dir << "/" << kMetadataFilename << ".json";
return tcb_ss.str();
return taichi::join_path(dir, std::string(kMetadataFilename) + ".json");
}

static std::vector<std::string> get_possible_llvm_cache_filename_by_key(
Expand Down Expand Up @@ -123,7 +109,7 @@ bool LlvmOfflineCacheFileReader::get_kernel_cache(

auto &kernel_data = itr->second;
if (kernel_data.owned_module == nullptr) {
const std::string filename_prefix = path_ + "/" + key;
const std::string filename_prefix = taichi::join_path(path_, key);
kernel_data.owned_module = load_module(filename_prefix, key, llvm_ctx);
TI_ASSERT(kernel_data.owned_module != nullptr);
kernel_data.module = kernel_data.owned_module.get();
Expand Down Expand Up @@ -165,9 +151,7 @@ void LlvmOfflineCacheFileWriter::dump(const std::string &path,

for (auto &[k, v] : data_.kernels) {
std::size_t size = 0; // bytes
std::stringstream filename_ss;
filename_ss << path << "/" << k;
std::string filename_prefix = filename_ss.str();
std::string filename_prefix = taichi::join_path(path, k);

auto write_llvm_module =
[&filename_prefix](
Expand Down Expand Up @@ -281,14 +265,13 @@ void LlvmOfflineCacheFileWriter::clean_cache(const std::string &path,

if ((policy & CleanOldVersion) &&
!is_current_llvm_cache_version(cache_data.version)) {
if (bool ok = fs::remove(get_llvm_cache_metadata_file_path(path)) &&
fs::remove(get_llvm_cache_metadata_json_file_path(path));
if (bool ok = taichi::remove(get_llvm_cache_metadata_file_path(path)) &&
taichi::remove(get_llvm_cache_metadata_json_file_path(path));
ok) {
auto root_path = fs::path(path);
for (const auto &[k, v] : cache_data.kernels) {
const auto files = get_possible_llvm_cache_filename_by_key(k);
for (const auto &f : files) {
fs::remove(root_path / f);
taichi::remove(taichi::join_path(path, f));
}
}
}
Expand Down Expand Up @@ -328,17 +311,16 @@ void LlvmOfflineCacheFileWriter::clean_cache(const std::string &path,
q.push(std::move(v));
}
TI_ASSERT(q.size() <= cnt);
auto root_path = fs::path(path);
while (!q.empty()) {
for (const auto &f :
get_possible_llvm_cache_filename_by_key(q.top().kernel_key)) {
fs::remove(root_path / f);
taichi::remove(taichi::join_path(path, f));
}
q.pop();
}
if (cnt == cache_data.kernels.size()) { // Removed all
fs::remove(get_llvm_cache_metadata_file_path(path));
fs::remove(get_llvm_cache_metadata_json_file_path(path));
taichi::remove(get_llvm_cache_metadata_file_path(path));
taichi::remove(get_llvm_cache_metadata_json_file_path(path));
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions taichi/util/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ inline void create_directories(const std::string &dir) {
#endif
}

template <typename First, typename... Path>
inline std::string join_path(First &&path, Path &&...others) {
if constexpr (sizeof...(others) == 0) {
return std::string(path);
} else {
return std::string(path) + "/" +
taichi::join_path(std::forward<Path>(others)...);
}
return "";
}

inline bool remove(const std::string &path) {
return std::remove(path.c_str()) == 0;
}

template <typename T>
void write_to_disk(const T &dat, std::string fn) {
FILE *f = fopen(fn.c_str(), "wb");
Expand Down
2 changes: 1 addition & 1 deletion tests/cpp/llvm/llvm_offline_cache_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace fs = std::filesystem;
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
error "Missing the <filesystem> header."
#error "Missing the <filesystem> header."
#endif // __has_include(<filesystem>)

#include "llvm/IR/IRBuilder.h"
Expand Down