Skip to content

Commit b7a955f

Browse files
committed
Cache compiler configuration probes
1 parent 869c683 commit b7a955f

4 files changed

Lines changed: 66 additions & 7 deletions

File tree

R/aaa-utils.R

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,30 @@ quickr_r_cmd <- function(
5454
r_cmd
5555
}
5656

57+
quickr_compiler_probe_cache <- new.env(parent = emptyenv())
58+
59+
quickr_cached_r_cmd_config_value <- function(
60+
name,
61+
cache = quickr_compiler_probe_cache
62+
) {
63+
stopifnot(is_string(name), is.environment(cache))
64+
65+
cached <- get0(name, envir = cache, inherits = FALSE, ifnotfound = NULL)
66+
if (is.null(cached)) {
67+
cached <- quickr_r_cmd_config_value(name)
68+
assign(name, cached, envir = cache)
69+
}
70+
71+
cached
72+
}
73+
5774
quickr_r_cmd_config_value <- function(
5875
name,
5976
r_cmd = quickr_r_cmd(),
6077
system2 = base::system2
6178
) {
79+
stopifnot(is_string(name), is_string(r_cmd))
80+
6281
out <- tryCatch(
6382
suppressWarnings(system2(
6483
r_cmd,
@@ -74,8 +93,9 @@ quickr_r_cmd_config_value <- function(
7493
}
7594
value <- trimws(paste(out, collapse = " "))
7695
if (!nzchar(value) || grepl("^ERROR:", value)) {
77-
return("")
96+
value <- ""
7897
}
98+
7999
value
80100
}
81101

R/compiler.R

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ quickr_flang_available <- function(
1515
system2 = base::system2
1616
) {
1717
flang <- quickr_flang_path(which = which)
18+
quickr_flang_available_at_path(flang, system2 = system2)
19+
}
20+
21+
quickr_flang_available_at_path <- function(
22+
flang,
23+
system2 = base::system2
24+
) {
25+
stopifnot(is_string(flang), is.function(system2))
26+
1827
if (!nzchar(flang)) {
1928
return(list(path = "", available = FALSE))
2029
}
@@ -28,6 +37,28 @@ quickr_flang_available <- function(
2837
list(path = flang, available = TRUE)
2938
}
3039

40+
quickr_cached_flang_available <- function(
41+
which = Sys.which,
42+
system2 = base::system2,
43+
cache = quickr_compiler_probe_cache
44+
) {
45+
stopifnot(is.function(which), is.function(system2), is.environment(cache))
46+
47+
flang <- quickr_flang_path(which = which)
48+
if (!identical(system2, base::system2)) {
49+
return(quickr_flang_available_at_path(flang, system2 = system2))
50+
}
51+
52+
cache_key <- paste("flang_available", flang, sep = "\r")
53+
cached <- get0(cache_key, envir = cache, inherits = FALSE, ifnotfound = NULL)
54+
if (is.null(cached)) {
55+
cached <- quickr_flang_available_at_path(flang)
56+
assign(cache_key, cached, envir = cache)
57+
}
58+
59+
cached
60+
}
61+
3162
quickr_flang_state <- local({
3263
state <- new.env(parent = emptyenv())
3364
state$auto_disabled <- FALSE
@@ -164,15 +195,15 @@ quickr_prefer_flang <- function(
164195

165196
# Best-effort: on macOS, prefer flang if it is available.
166197
if (sysname == "Darwin") {
167-
info <- quickr_flang_available(which = which, system2 = system2)
198+
info <- quickr_cached_flang_available(which = which, system2 = system2)
168199
return(isTRUE(info$available))
169200
}
170201

171202
FALSE
172203
}
173204

174205
quickr_default_fortran_makevars_lines <- function(
175-
config_value = quickr_r_cmd_config_value
206+
config_value = quickr_cached_r_cmd_config_value
176207
) {
177208
fc <- trimws(config_value("FC"))
178209
if (!nzchar(fc)) {
@@ -200,7 +231,7 @@ quickr_fcompiler_env <- function(
200231
sysname = Sys.info()[["sysname"]],
201232
use_openmp = FALSE,
202233
link_flags = character(),
203-
config_value = quickr_r_cmd_config_value
234+
config_value = quickr_cached_r_cmd_config_value
204235
) {
205236
stopifnot(is.character(build_dir), length(build_dir) == 1L, nzchar(build_dir))
206237

@@ -217,7 +248,10 @@ quickr_fcompiler_env <- function(
217248
system2 = system2
218249
))
219250
if (use_flang) {
220-
flang_info <- quickr_flang_available(which = which, system2 = system2)
251+
flang_info <- quickr_cached_flang_available(
252+
which = which,
253+
system2 = system2
254+
)
221255
flang <- flang_info$path
222256
if (!isTRUE(flang_info$available)) {
223257
if (isTRUE(explicit_request)) {

R/quick.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ compile <- function(fsub, build_dir = tempfile(paste0(fsub@name, "-build-"))) {
213213

214214
# Link against the same BLAS/LAPACK/Fortran libs as the running R
215215
# to support generated calls to vendor BLAS (e.g., dgemm, dgesv).
216-
cfg <- quickr_r_cmd_config_value
216+
cfg <- quickr_cached_r_cmd_config_value
217217
BLAS_LIBS <- strsplit(cfg("BLAS_LIBS"), "[[:space:]]+")[[1]]
218218
LAPACK_LIBS <- strsplit(cfg("LAPACK_LIBS"), "[[:space:]]+")[[1]]
219219
FLIBS <- strsplit(cfg("FLIBS"), "[[:space:]]+")[[1]]
@@ -326,7 +326,7 @@ compile <- function(fsub, build_dir = tempfile(paste0(fsub@name, "-build-"))) {
326326
quickr_windows_add_dll_paths <- function(
327327
flags,
328328
os_type = .Platform$OS.type,
329-
config_value = quickr_r_cmd_config_value,
329+
config_value = quickr_cached_r_cmd_config_value,
330330
which = Sys.which
331331
) {
332332
if (!identical(os_type, "windows")) {

tests/testthat/test-compiler.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ test_that("quickr_fortran_compiler_option validates values", {
131131
})
132132

133133
test_that("quickr_fcompiler_env writes Makevars when flang is usable", {
134+
rm(
135+
list = ls(envir = quickr_compiler_probe_cache, all.names = TRUE),
136+
envir = quickr_compiler_probe_cache
137+
)
138+
134139
temp <- withr::local_tempdir()
135140
prefix <- file.path(temp, "flang")
136141
dir.create(file.path(prefix, "bin"), recursive = TRUE)

0 commit comments

Comments
 (0)