Skip to content

Commit 6210425

Browse files
draganmladjenovichsharsha
authored andcommitted
Fix lldMain access to the LLVM command line
1 parent a1daec9 commit 6210425

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

tensorflow/compiler/mlir/tools/kernel_gen/hlo_to_kernel.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,17 @@ absl::StatusOr<std::string> EmitToBinary(llvm::StringRef host_triple,
127127
return ostream.str().str();
128128
}
129129

130-
absl::Status Run(llvm::StringRef input_file, llvm::StringRef output_file,
131-
llvm::StringRef host_triple,
132-
llvm::ArrayRef<std::string> architectures,
133-
llvm::ArrayRef<int64_t> tile_sizes,
134-
llvm::ArrayRef<int64_t> unroll_factors, bool print_ptx,
130+
absl::Status Run(std::string input_file, std::string output_file,
131+
std::string host_triple,
132+
std::vector<std::string> architectures,
133+
std::vector<int64_t> tile_sizes,
134+
std::vector<int64_t> unroll_factors, bool print_ptx,
135135
bool print_llvmir, bool enable_ftz, bool index_64bit,
136136
bool jit_compile, bool jit_i64_indexed_for_large_tensors) {
137137
// Read TF code.
138138
std::string hlo_code;
139139
TF_RETURN_IF_ERROR(
140-
ReadFileToString(Env::Default(), input_file.str(), &hlo_code));
140+
ReadFileToString(Env::Default(), input_file, &hlo_code));
141141

142142
// Compile.
143143
mlir::DialectRegistry registry;
@@ -160,7 +160,7 @@ absl::Status Run(llvm::StringRef input_file, llvm::StringRef output_file,
160160

161161
// Write .a file.
162162
TF_RETURN_IF_ERROR(
163-
WriteStringToFile(Env::Default(), output_file.str(), binary));
163+
WriteStringToFile(Env::Default(), output_file, binary));
164164
return absl::OkStatus();
165165
}
166166

third_party/xla/xla/service/gpu/llvm_gpu_backend/amdgpu_backend.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ void HsacoCache::Add(const std::string& ir, uint64_t hash,
194194
// TargetMachine for the AMDGPU target.
195195
absl::StatusOr<std::vector<uint8_t>> EmitModuleToHsaco(
196196
llvm::Module* module, llvm::TargetMachine* target_machine,
197-
const DebugOptions& debug_options) {
197+
const DebugOptions& debug_options,
198+
llvm_ir::LLVMCommandLineOptionsLock& llvm_lock) {
198199
auto* env = tsl::Env::Default();
199200
std::vector<std::string> tempdir_vector;
200201
env->GetLocalTempDirectories(&tempdir_vector);
@@ -258,8 +259,6 @@ absl::StatusOr<std::vector<uint8_t>> EmitModuleToHsaco(
258259

259260
if (debug_options.xla_gpu_use_inprocess_lld()) {
260261
#ifdef HAS_SUPPORT_FOR_LLD_AS_A_LIBRARY
261-
static absl::Mutex lld_mu(absl::kConstInit);
262-
263262
std::array<const char*, 7> args{
264263
"ld.lld", "--threads=1", "-shared",
265264
"--no-undefined", isabin_path.c_str(), "-o",
@@ -270,7 +269,7 @@ absl::StatusOr<std::vector<uint8_t>> EmitModuleToHsaco(
270269
llvm::raw_string_ostream os(error_message);
271270
lld::Result result;
272271
{
273-
absl::MutexLock lock(&lld_mu);
272+
llvm_lock.UpgradeToExclusiveAccessToRawLLVMCommandLine();
274273
result =
275274
lld::lldMain(args, llvm::nulls(), os, {{lld::Gnu, &lld::elf::link}});
276275
}
@@ -640,7 +639,7 @@ absl::StatusOr<std::vector<uint8_t>> CompileToHsaco(
640639

641640
// Lower optimized LLVM module to HSA code object.
642641
TF_ASSIGN_OR_RETURN(
643-
hsaco, EmitModuleToHsaco(module, target_machine.get(), debug_options));
642+
hsaco, EmitModuleToHsaco(module, target_machine.get(), debug_options, llvm_lock));
644643
HsacoCache::Add(str, hash, gcn_arch_name, hsaco);
645644
}
646645
return hsaco;

third_party/xla/xla/service/llvm_ir/llvm_command_line_options.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ limitations under the License.
2020
#include <vector>
2121

2222
#include "absl/algorithm/container.h"
23+
#include "absl/base/macros.h"
2324
#include "absl/base/thread_annotations.h"
2425
#include "absl/hash/hash.h"
2526
#include "absl/log/check.h"
@@ -89,5 +90,26 @@ LLVMCommandLineOptionsLock::~LLVMCommandLineOptionsLock() {
8990
num_active_clients_ -= 1;
9091
}
9192

93+
void LLVMCommandLineOptionsLock::
94+
UpgradeToExclusiveAccessToRawLLVMCommandLine() {
95+
{
96+
absl::MutexLock lock(lock_);
97+
CHECK_GT(num_active_clients_, 0);
98+
if (ABSL_PREDICT_TRUE(num_active_clients_ == 1)) {
99+
active_client_signature_ = 0;
100+
return;
101+
}
102+
num_active_clients_ -= 1;
103+
}
104+
// Slow path
105+
auto no_other_clients = []() ABSL_EXCLUSIVE_LOCKS_REQUIRED(lock_) {
106+
return num_active_clients_ == 0;
107+
};
108+
lock_.LockWhen(absl::Condition(&no_other_clients));
109+
active_client_signature_ = 0;
110+
num_active_clients_ = 1;
111+
lock_.unlock();
112+
}
113+
92114
} // namespace llvm_ir
93115
} // namespace xla

third_party/xla/xla/service/llvm_ir/llvm_command_line_options.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class ABSL_SCOPED_LOCKABLE LLVMCommandLineOptionsLock {
7676

7777
~LLVMCommandLineOptionsLock() ABSL_UNLOCK_FUNCTION();
7878

79+
void UpgradeToExclusiveAccessToRawLLVMCommandLine();
80+
7981
static std::vector<std::string>& GetGlobalOptions() {
8082
// absl::NoDestructor is not available in OSS XLA.
8183
static std::vector<std::string>* global_options =

0 commit comments

Comments
 (0)