-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathutils.jl
More file actions
122 lines (99 loc) · 3.4 KB
/
Copy pathutils.jl
File metadata and controls
122 lines (99 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
defs(mod::LLVM.Module) = filter(f -> !isdeclaration(f), collect(functions(mod)))
decls(mod::LLVM.Module) = filter(f -> isdeclaration(f) && !LLVM.isintrinsic(f),
collect(functions(mod)))
## timings
const to = TimerOutput()
timings() = (TimerOutputs.print_timer(to); println())
enable_timings() = (TimerOutputs.enable_debug_timings(GPUCompiler); return)
## debug verification
should_verify() = ccall(:jl_is_debugbuild, Cint, ()) == 1 ||
Base.JLOptions().debug_level >= 2 ||
parse(Bool, get(ENV, "CI", "false"))
## lazy module loading
using UUIDs
struct LazyModule
pkg::Base.PkgId
LazyModule(name, uuid) = new(Base.PkgId(uuid, name))
end
function Base.getproperty(lazy_mod::LazyModule, sym::Symbol)
pkg = getfield(lazy_mod, :pkg)
mod = get(Base.loaded_modules, pkg, nothing)
if mod === nothing
error("This functionality requires the $(pkg.name) package, which should be installed and loaded first.")
end
getfield(mod, sym)
end
## safe logging
using Logging
const STDERR_HAS_COLOR = Ref{Bool}(false)
# define safe loggers for use in generated functions (where task switches are not allowed)
for level in [:debug, :info, :warn, :error]
@eval begin
macro $(Symbol("safe_$level"))(ex...)
macrocall = :(@placeholder $(ex...))
# NOTE: `@placeholder` in order to avoid hard-coding @__LINE__ etc
macrocall.args[1] = Symbol($"@$level")
quote
old_logger = global_logger()
io = IOContext(Core.stderr, :color=>STDERR_HAS_COLOR[])
min_level = Logging.min_enabled_level(old_logger)
global_logger(Logging.ConsoleLogger(io, min_level))
ret = $(esc(macrocall))
global_logger(old_logger)
ret
end
end
end
end
## codegen locking
# lock codegen to prevent races on the LLVM context.
#
# XXX: it's not allowed to switch tasks while under this lock, can we guarantee that?
# its probably easier to start using our own LLVM context when that's possible.
macro locked(ex)
def = splitdef(ex)
def[:body] = quote
if VERSION >= v"1.9.0-DEV.1308"
ccall(:jl_typeinf_lock_begin, Cvoid, ())
else
ccall(:jl_typeinf_begin, Cvoid, ())
end
try
$(def[:body])
finally
if VERSION >= v"1.9.0-DEV.1308"
ccall(:jl_typeinf_lock_end, Cvoid, ())
else
ccall(:jl_typeinf_end, Cvoid, ())
end
end
end
esc(combinedef(def))
end
# HACK: temporarily unlock again to perform a task switch
macro unlocked(ex)
def = splitdef(ex)
def[:body] = quote
if VERSION >= v"1.9.0-DEV.1308"
ccall(:jl_typeinf_lock_end, Cvoid, ())
else
ccall(:jl_typeinf_end, Cvoid, ())
end
try
$(def[:body])
finally
if VERSION >= v"1.9.0-DEV.1308"
ccall(:jl_typeinf_lock_begin, Cvoid, ())
else
ccall(:jl_typeinf_begin, Cvoid, ())
end
end
end
esc(combinedef(def))
end
function callsite_attribute!(call, attributes)
# TODO: Make a nice API for this in LLVM.jl
for attribute in attributes
LLVM.API.LLVMAddCallSiteAttribute(call, LLVM.API.LLVMAttributeFunctionIndex, attribute)
end
end