Skip to content
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

Use pragmas to disable unused variable warnings in generated C++ #1422

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions src/stan_math_backend/Cpp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,6 @@
For (init, stop, incr, body)

let if_block cond stmts = IfElse (cond, block stmts, None)

(** Suppress warnings for a variable which may not be used. *)
let unused s =
[Comment "suppress unused var warning"; Expression (Cast (Void, Var s))]
end

module Decls = struct
Expand All @@ -274,13 +270,11 @@
VariableDefn
(make_variable_defn ~type_:Int ~name:"current_statement__"
~init:(Assignment (Literal "0")) ())
:: Stmts.unused "current_statement__"

let dummy_var =
VariableDefn
(make_variable_defn ~type_:Types.local_scalar ~name:"DUMMY_VAR__"
~init:(Construction [Exprs.quiet_NaN]) ())
:: Stmts.unused "DUMMY_VAR__"

let serializer_in =
VariableDefn
Expand Down Expand Up @@ -341,6 +335,7 @@
| Include of string
| IfNDef of string * defn list
| MacroApply of string * string list
| Pragma of string

(** The Stan model class always has a non-default constructor and
destructor *)
Expand All @@ -367,18 +362,23 @@
| Preprocessor of directive
[@@deriving sexp]

(* can't be derivided since it is simultaneously declared with non-records *)
let make_class_defn ~name ~public_base ?(final = true) ~private_members
~public_members ~constructor ?(destructor_body = []) () =
{ class_name= name
; public_base
; final
; private_members
; public_members
; constructor
; destructor_body }

let make_struct_defn ~param ~name ~body () = {param; struct_name= name; body}
module Defn = struct
(* can't be derived since it is simultaneously declared with non-records *)
let make_class ~name ~public_base ?(final = true) ~private_members

Check warning on line 367 in src/stan_math_backend/Cpp.ml

View check run for this annotation

Codecov / codecov/patch

src/stan_math_backend/Cpp.ml#L367

Added line #L367 was not covered by tests
~public_members ~constructor ?(destructor_body = []) () =
{ class_name= name
; public_base
; final
; private_members
; public_members
; constructor
; destructor_body }

let make_struct ~param ~name ~body () = {param; struct_name= name; body}

(** Some code relies on specific compiler pragmas *)
let if_not_MSVC defns = Preprocessor (IfNDef ("_MSC_VER", defns))
end

(** Much like in C++, we define a translation unit as a list of definitions *)
type program = defn list [@@deriving sexp]
Expand Down Expand Up @@ -631,6 +631,7 @@
pf ppf "@[<v>#ifndef %s@,%a@,#endif" name (list ~sep:cut pp_defn) defns
| MacroApply (name, args) ->
pf ppf "@[<h>%s(%a)@]" name (list ~sep:comma string) args
| Pragma s -> pf ppf "@[<h>#pragma %s@]" s

and pp_class_defn ppf
{ class_name
Expand Down
25 changes: 6 additions & 19 deletions src/stan_math_backend/Lower_functions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,12 @@ let lower_fun_body fdargs fdsuffix fdbody =
match fdsuffix with
| Fun_kind.FnLpdf _ | FnTarget | FnJacobian -> []
| FnPlain | FnRng ->
VariableDefn
(make_variable_defn ~static:true ~constexpr:true ~type_:Types.bool
~name:"propto__" ~init:(Assignment (Literal "true")) ())
:: Stmts.unused "propto__" in
[ VariableDefn
(make_variable_defn ~static:true ~constexpr:true ~type_:Types.bool
~name:"propto__" ~init:(Assignment (Literal "true")) ()) ] in
let body = lower_statement fdbody in
((local_scalar :: Decls.current_statement) @ to_refs)
@ propto @ Decls.dummy_var @ Stmts.rethrow_located body
(local_scalar :: Decls.current_statement :: to_refs)
@ propto @ [Decls.dummy_var] @ Stmts.rethrow_located body

let mk_extra_args templates args =
List.map
Expand Down Expand Up @@ -328,7 +327,7 @@ let lower_fun_def (functors : Lower_expr.variadic list)
make_fun_defn ~templates_init:([arg_templates], true) ~name:"operator()"
~return_type:(lower_returntype fdargs fdrt)
~args:cpp_args ~cv_qualifiers:[Const] ~body:defn_body () in
make_struct_defn ~param:struct_template ~name:functor_name
Defn.make_struct ~param:struct_template ~name:functor_name
~body:[FunDef functor_decl] () in
(fd, functors |> List.map ~f:register_functor)

Expand Down Expand Up @@ -465,16 +464,10 @@ module Testing = struct
using local_scalar_t__ = stan::promote_args_t<stan::base_type_t<T0__>,
stan::base_type_t<T1__>>;
int current_statement__ = 0;
// suppress unused var warning
(void) current_statement__;
const auto& x = stan::math::to_ref(x_arg__);
const auto& y = stan::math::to_ref(y_arg__);
static constexpr bool propto__ = true;
// suppress unused var warning
(void) propto__;
local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
// suppress unused var warning
(void) DUMMY_VAR__;
try {
return stan::math::add(x, 1);
} catch (const std::exception& e) {
Expand Down Expand Up @@ -538,17 +531,11 @@ module Testing = struct
stan::base_type_t<T2__>,
stan::base_type_t<T3__>>;
int current_statement__ = 0;
// suppress unused var warning
(void) current_statement__;
const auto& x = stan::math::to_ref(x_arg__);
const auto& y = stan::math::to_ref(y_arg__);
const auto& z = stan::math::to_ref(z_arg__);
static constexpr bool propto__ = true;
// suppress unused var warning
(void) propto__;
local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
// suppress unused var warning
(void) DUMMY_VAR__;
try {
return stan::math::add(x, 1);
} catch (const std::exception& e) {
Expand Down
82 changes: 42 additions & 40 deletions src/stan_math_backend/Lower_program.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,30 @@ let get_unconstrained_param_st lst =
@param fname Name of the function.
*)
let gen_function__ prog_name fname =
[ VariableDefn
(make_variable_defn ~static:true ~constexpr:true
~type_:(Const (Pointer (TypeLiteral "char"))) ~name:"function__"
~init:
(Assignment
(Exprs.literal_string (prog_name ^ "_namespace::" ^ fname)))
()) ]
@ Stmts.unused "function__"
VariableDefn
(make_variable_defn ~static:true ~constexpr:true
~type_:(Const (Pointer (TypeLiteral "char"))) ~name:"function__"
~init:
(Assignment (Exprs.literal_string (prog_name ^ "_namespace::" ^ fname)))
())

let version = GlobalComment "Code generated by %%NAME%% %%VERSION%%"
let includes = Preprocessor (Include "stan/model/model_header.hpp")

let prevent_unused =
Defn.if_not_MSVC
[ GlobalComment
"Disable warnings for unused variables in our generated code."
; Preprocessor (Pragma "GCC diagnostic push")
; Preprocessor
(Pragma {|GCC diagnostic ignored "-Wunknown-warning-option"|})
; Preprocessor
(Pragma {|GCC diagnostic ignored "-Wunused-but-set-variable"|})
; Preprocessor (Pragma {|GCC diagnostic ignored "-Wunused-variable"|}) ]

let diagnostics_pop =
[Defn.if_not_MSVC [Preprocessor (Pragma "GCC diagnostic pop")]]

(** Generate the private members of the model class

This accounts for types that can be moved to OpenCL.
Expand Down Expand Up @@ -156,18 +168,14 @@ let lower_constructor
; (TypeLiteral "unsigned int", "random_seed__ = 0")
; (Pointer (TypeLiteral "std::ostream"), "pstream__ = nullptr") ] in
let preamble =
Decls.current_statement
@ [ Using ("local_scalar_t__", Some Double)
; VariableDefn
(make_variable_defn ~type_:(TypeLiteral "auto") ~name:"base_rng__"
~init:
(Assignment
(Exprs.fun_call "stan::services::util::create_rng"
[Var "random_seed__"; Literal "0"]))
()) ]
@ Stmts.unused "base_rng__"
@ gen_function__ prog_name prog_name
@ Decls.dummy_var in
[ Decls.current_statement; Using ("local_scalar_t__", Some Double)
; VariableDefn
(make_variable_defn ~type_:(TypeLiteral "auto") ~name:"base_rng__"
~init:
(Assignment
(Exprs.fun_call "stan::services::util::create_rng"
[Var "random_seed__"; Literal "0"]))
()); gen_function__ prog_name prog_name; Decls.dummy_var ] in
let data_idents = List.map ~f:fst3 input_vars |> String.Set.of_list in
let lower_data (Stmt.Fixed.{pattern; meta} as s) =
match pattern with
Expand Down Expand Up @@ -217,9 +225,8 @@ let gen_log_prob Program.{prog_name; log_prob; reverse_mode_log_prob; _} =
; VariableDefn
(make_variable_defn ~type_:t__ ~name:"lp__"
~init:(Construction [Literal "0.0"]) ()); Decls.lp_accum t__
; Decls.serializer_in ]
@ Decls.current_statement @ Decls.dummy_var
@ gen_function__ prog_name "log_prob" in
; Decls.serializer_in; Decls.current_statement; Decls.dummy_var
; gen_function__ prog_name "log_prob" ] in
let outro =
let open Expression_syntax in
let lp_accum__ = Var "lp_accum__" in
Expand Down Expand Up @@ -266,19 +273,15 @@ let gen_write_array {Program.prog_name; generate_quantities; _} =
; Decls.serializer_out
; VariableDefn
(make_variable_defn ~static:true ~constexpr:true ~type_:Types.bool
~name:"propto__" ~init:(Assignment (Literal "true")) ()) ]
@ Stmts.unused "propto__"
@ VariableDefn
~name:"propto__" ~init:(Assignment (Literal "true")) ())
; VariableDefn
(make_variable_defn ~type_:Double ~name:"lp__"
~init:(Assignment (Literal "0.0")) ())
:: Stmts.unused "lp__"
@ Decls.current_statement
@ (Decls.lp_accum Double :: Decls.dummy_var)
@ VariableDefn
~init:(Assignment (Literal "0.0")) ()); Decls.current_statement
; Decls.lp_accum Double; Decls.dummy_var
; VariableDefn
(make_variable_defn ~constexpr:true ~type_:Types.bool ~name:"jacobian__"
~init:(Assignment (Literal "false")) ())
:: Stmts.unused "jacobian__"
@ gen_function__ prog_name "write_array" in
; gen_function__ prog_name "write_array" ] in
FunDef
(make_fun_defn ~templates_init:([templates], true) ~inline:true
~return_type:Void ~name:"write_array_impl" ~args
Expand All @@ -294,9 +297,8 @@ let gen_transform_inits_impl {Program.transform_inits; output_vars; _} =
; (Ref (TemplateType "VecVar"), "vars__")
; (Pointer (TypeLiteral "std::ostream"), "pstream__ = nullptr") ] in
let intro =
Using ("local_scalar_t__", Some Double)
:: Decls.serializer_out :: Decls.current_statement
@ Decls.dummy_var in
[ Using ("local_scalar_t__", Some Double); Decls.serializer_out
; Decls.current_statement; Decls.dummy_var ] in
let validate_params
( (name : string)
, (loc : int)
Expand Down Expand Up @@ -330,8 +332,7 @@ let gen_unconstrain_array_impl {Program.unconstrain_array; _} =
; (Pointer (TypeLiteral "std::ostream"), "pstream__ = nullptr") ] in
let intro =
[ Using ("local_scalar_t__", Some Double); Decls.serializer_in
; Decls.serializer_out ]
@ Decls.current_statement @ Decls.dummy_var in
; Decls.serializer_out; Decls.current_statement; Decls.dummy_var ] in
FunDef
(make_fun_defn ~templates_init:([templates], true) ~inline:true
~return_type:Void ~name:"unconstrain_array_impl" ~args
Expand Down Expand Up @@ -830,7 +831,7 @@ let lower_model ({Program.prog_name; _} as p) =
let public_members = model_public_basics prog_name @ lower_model_public p in
let constructor = lower_constructor p in
Class
(make_class_defn ~name:prog_name ~final:true
(Defn.make_class ~name:prog_name ~final:true
~public_base:(TypeTrait ("model_base_crtp", [TypeLiteral prog_name]))
~private_members ~public_members ~constructor ())

Expand Down Expand Up @@ -893,7 +894,8 @@ let lower_program ?printed_filename (p : Program.Typed.t) : Cpp.program =
new_model_boilerplate p.prog_name
@ Numbering.register_map_rect_functors model_namespace_str map_rect_calls
in
[version; includes; model_namespace] @ global_fns
[version; includes; prevent_unused; model_namespace]
@ global_fns @ diagnostics_pop

module Testing = struct
open Fmt
Expand Down
Loading