Skip to content

Commit

Permalink
properly free resource
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenzek committed Apr 20, 2023
1 parent a7eab01 commit 45c080c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
14 changes: 12 additions & 2 deletions src/codegen/llvm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ pub const Object = struct {
/// second extern Decl could not be emitted with the correct name due to a
/// name collision.
extern_collisions: std.AutoArrayHashMapUnmanaged(Module.Decl.Index, void),
/// Optimization remarks file handler. When outputting optimization remarks,
/// LLVM expect us to properly close this resource.
opt_remarks: ?*llvm.ToolOutputFile,

pub const TypeMap = std.HashMapUnmanaged(
Type,
Expand Down Expand Up @@ -523,12 +526,17 @@ pub const Object = struct {
context.setOptBisectLimit(std.math.lossyCast(c_int, options.opt_bisect_limit));
}

var opt_remarks: ?*llvm.ToolOutputFile = null;
if (options.opt_remarks_emit) |emit| {
var sub_path = try gpa.dupeZ(u8, emit.sub_path);
var opt_remarks_emit_path = try emit.basenamePath(gpa, sub_path);
defer gpa.free(opt_remarks_emit_path);
var setup_err = context.setupOptimizationRemarks(opt_remarks_emit_path, ".*");
assert(setup_err == 0);

opt_remarks = context.openOptimizationRemarks(opt_remarks_emit_path);
if (opt_remarks == null) {
log.err("LLVM failed to open opt remarks file: {s}", .{opt_remarks_emit_path});
return error.FailedToEmit;
}
}

return Object{
Expand All @@ -549,6 +557,7 @@ pub const Object = struct {
.di_type_map = .{},
.error_name_table = null,
.extern_collisions = .{},
.opt_remarks = opt_remarks,
};
}

Expand All @@ -567,6 +576,7 @@ pub const Object = struct {
self.type_map.deinit(gpa);
self.type_map_arena.deinit();
self.extern_collisions.deinit(gpa);
if (self.opt_remarks) |remarks_file| remarks_file.close();
self.* = undefined;
}

Expand Down
9 changes: 7 additions & 2 deletions src/codegen/llvm/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,13 @@ pub const Context = opaque {
pub const setOptBisectLimit = ZigLLVMSetOptBisectLimit;
extern fn ZigLLVMSetOptBisectLimit(C: *Context, limit: c_int) void;

pub const setupOptimizationRemarks = ZigLLVMSetupOptimizationRemarks;
extern fn ZigLLVMSetupOptimizationRemarks(C: *Context, RemarksFilename: ?[*:0]const u8, RemarksPasses: ?[*:0]const u8) i32;
pub const openOptimizationRemarks = ZigLLVMOpenOptimizationRemarks;
extern fn ZigLLVMOpenOptimizationRemarks(C: *Context, RemarksFilename: [*:0]const u8) *ToolOutputFile;
};

pub const ToolOutputFile = opaque {
pub const close = ZigLLVMCloseToolOutputFile;
extern fn ZigLLVMCloseToolOutputFile(file: *ToolOutputFile) void;
};

pub const Value = opaque {
Expand Down
2 changes: 1 addition & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1339,7 +1339,7 @@ fn buildOutputType(
} else if (mem.eql(u8, arg, "-fno-emit-implib")) {
emit_implib = .no;
emit_implib_arg_provided = true;
} else if (mem.eql(u8, arg, "-femit-opt-remarks")) {
} else if (mem.eql(u8, arg, "-femit-opt-remarks")) {
emit_opt_remarks = .yes_default_path;
} else if (mem.startsWith(u8, arg, "-femit-opt-remarks=")) {
emit_opt_remarks = .{ .yes = arg["-femit-opt-remarks=".len..] };
Expand Down
24 changes: 14 additions & 10 deletions src/zig_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,18 +426,22 @@ ZIG_EXTERN_C void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit
// unwrap(context_ref)->setOptPassGate(_opt_bisector);
}

ZIG_EXTERN_C int ZigLLVMSetupOptimizationRemarks(LLVMContextRef context_ref, const char *RemarksFilename, const char *RemarksPasses) {
auto RemarksFileOrErr = llvm::setupLLVMOptimizationRemarks(*unwrap(context_ref), RemarksFilename, RemarksPasses, "yaml", false, 0);
if (Error E = RemarksFileOrErr.takeError())
return 1;

std::unique_ptr<ToolOutputFile> RemarksFile = std::move(*RemarksFileOrErr);
ZIG_EXTERN_C ZigLLVMToolOutputFile* ZigLLVMOpenOptimizationRemarks(LLVMContextRef context_ref, const char *RemarksFilename) {
auto remarks_err = llvm::setupLLVMOptimizationRemarks(*unwrap(context_ref), RemarksFilename, ".*", "yaml", false, 0);
if (Error E = remarks_err.takeError())
return nullptr;

if (RemarksFile) {
RemarksFile->keep();
RemarksFile.release();
std::unique_ptr<ToolOutputFile> remarks_file = std::move(*remarks_err);
if (remarks_file) {
remarks_file->keep();
return reinterpret_cast<ZigLLVMToolOutputFile *>(remarks_file.release());
}
return 0;
return nullptr;
}

ZIG_EXTERN_C void ZigLLVMCloseToolOutputFile(ZigLLVMToolOutputFile *remarks_file) {
ToolOutputFile* file = reinterpret_cast<ToolOutputFile*>(remarks_file);
delete file;
}

LLVMValueRef ZigLLVMAddFunctionInAddressSpace(LLVMModuleRef M, const char *Name, LLVMTypeRef FunctionTy, unsigned AddressSpace) {
Expand Down
5 changes: 4 additions & 1 deletion src/zig_llvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ struct ZigLLVMDIEnumerator;
struct ZigLLVMInsertionPoint;
struct ZigLLVMDINode;
struct ZigLLVMMDString;
struct ZigLLVMToolOutputFile;


ZIG_EXTERN_C void ZigLLVMInitializeLoopStrengthReducePass(LLVMPassRegistryRef R);
ZIG_EXTERN_C void ZigLLVMInitializeLowerIntrinsicsPass(LLVMPassRegistryRef R);
Expand Down Expand Up @@ -69,7 +71,8 @@ ZIG_EXTERN_C LLVMTypeRef ZigLLVMTokenTypeInContext(LLVMContextRef context_ref);

ZIG_EXTERN_C void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit);

ZIG_EXTERN_C int ZigLLVMSetupOptimizationRemarks(LLVMContextRef context_ref, const char *RemarksFilename, const char *RemarksPasses);
ZIG_EXTERN_C ZigLLVMToolOutputFile* ZigLLVMOpenOptimizationRemarks(LLVMContextRef context_ref, const char *RemarksFilename);
ZIG_EXTERN_C void ZigLLVMCloseToolOutputFile(ZigLLVMToolOutputFile *remarks_file);

ZIG_EXTERN_C LLVMValueRef ZigLLVMAddFunctionInAddressSpace(LLVMModuleRef M, const char *Name,
LLVMTypeRef FunctionTy, unsigned AddressSpace);
Expand Down

0 comments on commit 45c080c

Please sign in to comment.