-
Notifications
You must be signed in to change notification settings - Fork 8
Add error handling plumbing #86
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
Changes from 2 commits
c5ed31d
6459cc1
14eba24
f5cb4a0
aa0a800
16bcb7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| quickr_error_msg_name <- function() "quickr_err_msg" | ||
|
|
||
| quickr_error_msg_len <- function() 256L | ||
|
|
||
| quickr_error_setter_name <- function() "quickr_set_error_msg" | ||
|
|
||
| quickr_error_arg_names <- function() { | ||
| c(quickr_error_msg_name()) | ||
| } | ||
|
|
||
| is_quickr_error_msg <- function(name) { | ||
| identical(name, quickr_error_msg_name()) | ||
| } | ||
|
|
||
| scope_root_for_errors <- function(scope) { | ||
| if (!inherits(scope, "quickr_scope")) { | ||
| return(scope) | ||
| } | ||
| while ( | ||
| !identical(attr(scope, "kind", exact = TRUE), "subroutine") && | ||
| inherits(parent.env(scope), "quickr_scope") | ||
| ) { | ||
| scope <- parent.env(scope) | ||
| } | ||
| scope | ||
| } | ||
|
|
||
| mark_scope_uses_errors <- function(scope) { | ||
| root <- scope_root_for_errors(scope) | ||
| if (inherits(root, "quickr_scope")) { | ||
| attr(root, "uses_errors") <- TRUE | ||
| } | ||
| invisible(TRUE) | ||
| } | ||
|
|
||
| scope_uses_errors <- function(scope) { | ||
| root <- scope_root_for_errors(scope) | ||
| isTRUE(attr(root, "uses_errors", TRUE)) | ||
| } | ||
|
|
||
| fortran_string_literal <- function(x) { | ||
| stopifnot(is_string(x)) | ||
| escaped <- gsub("\r\n|\r|\n", "\\\\n", x) | ||
| escaped <- gsub("\"", "\"\"", escaped, fixed = TRUE) | ||
| paste0("\"", escaped, "\"") | ||
| } | ||
|
|
||
| check_quickr_error_message_continuable <- function(msg) { | ||
| stopifnot(is_string(msg)) | ||
| if (grepl("[ \t]", msg)) { | ||
| return(invisible(TRUE)) | ||
| } | ||
| msg_literal <- fortran_string_literal(msg) | ||
| line_len <- nchar(glue( | ||
| "&{quickr_error_setter_name()}( {msg_literal} )" | ||
| )) | ||
| if (line_len > 132L) { | ||
| stop( | ||
| "Error message is too long to fit in a single Fortran line without spaces.", | ||
| " Add spaces to allow line continuations.", | ||
| call. = FALSE | ||
| ) | ||
| } | ||
| invisible(TRUE) | ||
| } | ||
|
|
||
| quickr_error_manifest_lines <- function() { | ||
| msg_name <- quickr_error_msg_name() | ||
| len_val <- quickr_error_msg_len() | ||
|
|
||
| glue("character(kind=c_char), intent(inout) :: {msg_name}({len_val})") | ||
| } | ||
|
|
||
| quickr_error_helper_fortran <- function(openmp = FALSE) { | ||
| msg_name <- quickr_error_msg_name() | ||
| setter <- quickr_error_setter_name() | ||
| len_val <- quickr_error_msg_len() | ||
|
|
||
| glue::trim(str_flatten_lines( | ||
| glue("subroutine {setter}(msg)"), | ||
| if (isTRUE(openmp)) " use omp_lib, only: omp_in_parallel", | ||
| " character(len=*), intent(in) :: msg", | ||
| " integer :: i", | ||
| " integer :: n", | ||
| if (isTRUE(openmp)) " !$omp critical (quickr_error)", | ||
| glue(" if ({msg_name}(1) == c_null_char) then"), | ||
| glue(" n = min(len(msg), {len_val} - 1)"), | ||
| glue(" {msg_name}(1:n) = [(msg(i:i), i = 1, n)]"), | ||
| glue(" {msg_name}(n + 1) = c_null_char"), | ||
| " end if", | ||
| if (isTRUE(openmp)) " !$omp end critical (quickr_error)", | ||
| if (isTRUE(openmp)) " if (omp_in_parallel()) then", | ||
| if (isTRUE(openmp)) " !$omp cancel parallel", | ||
| if (isTRUE(openmp)) " end if", | ||
| glue("end subroutine {setter}") | ||
| )) | ||
| } | ||
|
|
||
| quickr_error_fortran_lines <- function(message = NULL) { | ||
| msg <- message %||% "quickr error" | ||
| stopifnot(is_string(msg)) | ||
| if (!nzchar(msg)) { | ||
| msg <- "quickr error" | ||
| } | ||
| check_quickr_error_message_continuable(msg) | ||
| msg_literal <- fortran_string_literal(msg) | ||
| c(glue("call {quickr_error_setter_name()}({msg_literal})"), "return") | ||
| } | ||
|
|
||
| quickr_error_return_if_set <- function(scope) { | ||
| if (isTRUE(scope_uses_errors(scope))) { | ||
| glue("if ({quickr_error_msg_name()}(1) /= c_null_char) return") | ||
| } else { | ||
| "" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -467,9 +467,16 @@ compile_closure_call <- function( | |
|
|
||
| call_args <- unname(args_f) | ||
| if (length(call_args)) { | ||
| return(Fortran(glue("call {proc$name}({str_flatten_commas(call_args)})"))) | ||
| call_stmt <- glue("call {proc$name}({str_flatten_commas(call_args)})") | ||
| return(Fortran(str_flatten_lines( | ||
| call_stmt, | ||
| quickr_error_return_if_set(scope) | ||
| ))) | ||
| } | ||
| return(Fortran(glue("call {proc$name}()"))) | ||
| return(Fortran(str_flatten_lines( | ||
| glue("call {proc$name}()"), | ||
| quickr_error_return_if_set(scope) | ||
| ))) | ||
| } | ||
|
|
||
| proc <- compile_local_closure_proc( | ||
|
|
@@ -484,9 +491,16 @@ compile_closure_call <- function( | |
| if (!needs_value && is.null(proc$res)) { | ||
| call_args <- unname(args_f) | ||
| if (length(call_args)) { | ||
| return(Fortran(glue("call {proc$name}({str_flatten_commas(call_args)})"))) | ||
| call_stmt <- glue("call {proc$name}({str_flatten_commas(call_args)})") | ||
| return(Fortran(str_flatten_lines( | ||
| call_stmt, | ||
| quickr_error_return_if_set(scope) | ||
| ))) | ||
| } | ||
| return(Fortran(glue("call {proc$name}()"))) | ||
| return(Fortran(str_flatten_lines( | ||
| glue("call {proc$name}()"), | ||
| quickr_error_return_if_set(scope) | ||
| ))) | ||
| } | ||
|
|
||
| res_var <- proc$res_var | ||
|
|
@@ -497,6 +511,7 @@ compile_closure_call <- function( | |
| tmp <- hoist$declare_tmp(mode = res_var@mode, dims = res_var@dims) | ||
| call_args <- c(unname(args_f), tmp@name) | ||
| call_stmt <- glue("call {proc$name}({str_flatten_commas(call_args)})") | ||
| call_stmt <- str_flatten_lines(call_stmt, quickr_error_return_if_set(scope)) | ||
|
|
||
| if (needs_value) { | ||
| hoist$emit(call_stmt) | ||
|
|
@@ -621,6 +636,7 @@ compile_closure_call_assignment <- function( | |
| Fortran(glue( | ||
| " | ||
| call {proc$name}({str_flatten_commas(call_args)}) | ||
| {quickr_error_return_if_set(scope)} | ||
| {str_flatten_lines(post)} | ||
| " | ||
| )) | ||
|
|
@@ -863,6 +879,16 @@ compile_sapply_assignment <- function( | |
| if (!is.null(parallel)) { | ||
| mark_openmp_used(scope) | ||
| } | ||
| error_check_inner <- if (is.null(parallel)) { | ||
| quickr_error_return_if_set(scope) | ||
| } else { | ||
| "" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| } | ||
| error_check_after <- if (!is.null(parallel)) { | ||
| quickr_error_return_if_set(scope) | ||
| } else { | ||
| "" | ||
| } | ||
| loop_header <- glue("do {idx@name} = 1_c_int, {last_i}") | ||
| prefix <- str_flatten_lines( | ||
| if (!index_iterable) iterable_tmp_assign else NULL, | ||
|
|
@@ -872,8 +898,10 @@ compile_sapply_assignment <- function( | |
| " | ||
| {prefix} | ||
| call {proc_name}({call_args}) | ||
| {error_check_inner} | ||
| end do | ||
| {str_flatten_lines(directives$suffix)} | ||
| {error_check_after} | ||
| {str_flatten_lines(post_stmts)} | ||
| " | ||
| )) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The helper always emits a bare
returnafter setting the error message. If a user callsstop()(or triggers the Lapack error helpers that reuse this) inside aparallel()loop, the generated code places thatreturninside a!$omp parallel doregion, which OpenMP forbids as a branch out of a parallel region and typically fails to compile. Consider suppressing thereturnwhen OpenMP is active and rely on!$omp cancel parallelplus the post-loop error check, or emit a loop-localcycleinstead.Useful? React with 👍 / 👎.