diff options
author | Philip Herron <philip.herron@embecosm.com> | 2022-03-09 15:35:04 +0000 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2022-03-10 11:40:11 +0000 |
commit | 94990a843b6df9ff6010957d724dbb70bea94ceb (patch) | |
tree | 4a8668a80af92f29d659063b8e2f6ccb24ed4f13 /gcc | |
parent | a08ac0c27aed0c6d390d03656b708eb71381a5e9 (diff) | |
download | gcc-94990a843b6df9ff6010957d724dbb70bea94ceb.zip gcc-94990a843b6df9ff6010957d724dbb70bea94ceb.tar.gz gcc-94990a843b6df9ff6010957d724dbb70bea94ceb.tar.bz2 |
Refactor how we define simple intrinsics
Intrinsics were hidden behind the GCC abstract. This removes it by keeping
all of this logic within rust-intrinsic.cc so that we can make mappings of
the rustc name to GCC ones. We have a big comment from the mappings used
over to LLVM builtins which we can use to help guide how we do this for
GCC.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/backend/rust-compile-intrinsic.cc | 197 | ||||
-rw-r--r-- | gcc/rust/rust-backend.h | 6 | ||||
-rw-r--r-- | gcc/rust/rust-gcc.cc | 80 |
3 files changed, 143 insertions, 140 deletions
diff --git a/gcc/rust/backend/rust-compile-intrinsic.cc b/gcc/rust/backend/rust-compile-intrinsic.cc index 177c14e..c57943c 100644 --- a/gcc/rust/backend/rust-compile-intrinsic.cc +++ b/gcc/rust/backend/rust-compile-intrinsic.cc @@ -15,6 +15,7 @@ // <http://www.gnu.org/licenses/>. #include "rust-compile-intrinsic.h" +#include "langhooks.h" #include "rust-compile-type.h" #include "rust-compile-fnparam.h" #include "rust-tree.h" @@ -22,6 +23,144 @@ namespace Rust { namespace Compile { +// https://github.com/rust-lang/rust/blob/master/library/core/src/intrinsics.rs +// https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_llvm/src/intrinsic.rs +// https://github.com/Rust-GCC/gccrs/issues/658 +// +// let llvm_name = match name { +// sym::sqrtf32 => "llvm.sqrt.f32", +// sym::sqrtf64 => "llvm.sqrt.f64", +// sym::powif32 => "llvm.powi.f32", +// sym::powif64 => "llvm.powi.f64", +// sym::sinf32 => "llvm.sin.f32", +// sym::sinf64 => "llvm.sin.f64", +// sym::cosf32 => "llvm.cos.f32", +// sym::cosf64 => "llvm.cos.f64", +// sym::powf32 => "llvm.pow.f32", +// sym::powf64 => "llvm.pow.f64", +// sym::expf32 => "llvm.exp.f32", +// sym::expf64 => "llvm.exp.f64", +// sym::exp2f32 => "llvm.exp2.f32", +// sym::exp2f64 => "llvm.exp2.f64", +// sym::logf32 => "llvm.log.f32", +// sym::logf64 => "llvm.log.f64", +// sym::log10f32 => "llvm.log10.f32", +// sym::log10f64 => "llvm.log10.f64", +// sym::log2f32 => "llvm.log2.f32", +// sym::log2f64 => "llvm.log2.f64", +// sym::fmaf32 => "llvm.fma.f32", +// sym::fmaf64 => "llvm.fma.f64", +// sym::fabsf32 => "llvm.fabs.f32", +// sym::fabsf64 => "llvm.fabs.f64", +// sym::minnumf32 => "llvm.minnum.f32", +// sym::minnumf64 => "llvm.minnum.f64", +// sym::maxnumf32 => "llvm.maxnum.f32", +// sym::maxnumf64 => "llvm.maxnum.f64", +// sym::copysignf32 => "llvm.copysign.f32", +// sym::copysignf64 => "llvm.copysign.f64", +// sym::floorf32 => "llvm.floor.f32", +// sym::floorf64 => "llvm.floor.f64", +// sym::ceilf32 => "llvm.ceil.f32", +// sym::ceilf64 => "llvm.ceil.f64", +// sym::truncf32 => "llvm.trunc.f32", +// sym::truncf64 => "llvm.trunc.f64", +// sym::rintf32 => "llvm.rint.f32", +// sym::rintf64 => "llvm.rint.f64", +// sym::nearbyintf32 => "llvm.nearbyint.f32", +// sym::nearbyintf64 => "llvm.nearbyint.f64", +// sym::roundf32 => "llvm.round.f32", +// sym::roundf64 => "llvm.round.f64", +// _ => return None, +// }; +// Some(cx.get_intrinsic(&llvm_name)) +class SimpleIntrinsics +{ +public: + static SimpleIntrinsics &get () + { + static SimpleIntrinsics instance; + return instance; + } + + bool lookup_simple_builtin (const std::string &name, tree *builtin) + { + auto it = rust_intrinsic_to_gcc_builtin.find (name); + if (it == rust_intrinsic_to_gcc_builtin.end ()) + return false; + + return lookup_gcc_builtin (it->second, builtin); + } + +private: + static const int builtin_const = 1 << 0; + static const int builtin_noreturn = 1 << 1; + static const int builtin_novops = 1 << 2; + + SimpleIntrinsics () { setup (); } + + void setup () + { + tree math_function_type_f32 + = build_function_type_list (float_type_node, float_type_node, NULL_TREE); + + define_builtin ("sinf32", BUILT_IN_SINF, "__builtin_sinf", "sinf", + math_function_type_f32, builtin_const); + + define_builtin ("sqrtf32", BUILT_IN_SQRTF, "__builtin_sqrtf", "sqrtf", + math_function_type_f32, builtin_const); + } + + // Define a builtin function. BCODE is the builtin function code + // defined by builtins.def. NAME is the name of the builtin function. + // LIBNAME is the name of the corresponding library function, and is + // NULL if there isn't one. FNTYPE is the type of the function. + // CONST_P is true if the function has the const attribute. + // NORETURN_P is true if the function has the noreturn attribute. + void define_builtin (const std::string rust_name, built_in_function bcode, + const char *name, const char *libname, tree fntype, + int flags) + { + tree decl = add_builtin_function (name, fntype, bcode, BUILT_IN_NORMAL, + libname, NULL_TREE); + if ((flags & builtin_const) != 0) + TREE_READONLY (decl) = 1; + if ((flags & builtin_noreturn) != 0) + TREE_THIS_VOLATILE (decl) = 1; + if ((flags & builtin_novops) != 0) + DECL_IS_NOVOPS (decl) = 1; + set_builtin_decl (bcode, decl, true); + this->builtin_functions_[name] = decl; + if (libname != NULL) + { + decl = add_builtin_function (libname, fntype, bcode, BUILT_IN_NORMAL, + NULL, NULL_TREE); + if ((flags & builtin_const) != 0) + TREE_READONLY (decl) = 1; + if ((flags & builtin_noreturn) != 0) + TREE_THIS_VOLATILE (decl) = 1; + if ((flags & builtin_novops) != 0) + DECL_IS_NOVOPS (decl) = 1; + this->builtin_functions_[libname] = decl; + } + + rust_intrinsic_to_gcc_builtin[rust_name] = name; + } + + bool lookup_gcc_builtin (const std::string &name, tree *builtin) + { + auto it = builtin_functions_.find (name); + if (it == builtin_functions_.end ()) + return false; + + *builtin = it->second; + return true; + } + + // A mapping of the GCC built-ins exposed to GCC Rust. + std::map<std::string, tree> builtin_functions_; + std::map<std::string, std::string> rust_intrinsic_to_gcc_builtin; +}; + static tree offset_intrinsic_handler (Context *ctx, TyTy::BaseType *fntype); @@ -36,60 +175,10 @@ Intrinsics::compile (TyTy::FnType *fntype) { rust_assert (fntype->get_abi () == ABI::INTRINSIC); - // https://github.com/rust-lang/rust/blob/master/library/core/src/intrinsics.rs - // https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_llvm/src/intrinsic.rs - // https://github.com/Rust-GCC/gccrs/issues/658 - - // let llvm_name = match name { - // sym::sqrtf32 => "llvm.sqrt.f32", - // sym::sqrtf64 => "llvm.sqrt.f64", - // sym::powif32 => "llvm.powi.f32", - // sym::powif64 => "llvm.powi.f64", - // sym::sinf32 => "llvm.sin.f32", - // sym::sinf64 => "llvm.sin.f64", - // sym::cosf32 => "llvm.cos.f32", - // sym::cosf64 => "llvm.cos.f64", - // sym::powf32 => "llvm.pow.f32", - // sym::powf64 => "llvm.pow.f64", - // sym::expf32 => "llvm.exp.f32", - // sym::expf64 => "llvm.exp.f64", - // sym::exp2f32 => "llvm.exp2.f32", - // sym::exp2f64 => "llvm.exp2.f64", - // sym::logf32 => "llvm.log.f32", - // sym::logf64 => "llvm.log.f64", - // sym::log10f32 => "llvm.log10.f32", - // sym::log10f64 => "llvm.log10.f64", - // sym::log2f32 => "llvm.log2.f32", - // sym::log2f64 => "llvm.log2.f64", - // sym::fmaf32 => "llvm.fma.f32", - // sym::fmaf64 => "llvm.fma.f64", - // sym::fabsf32 => "llvm.fabs.f32", - // sym::fabsf64 => "llvm.fabs.f64", - // sym::minnumf32 => "llvm.minnum.f32", - // sym::minnumf64 => "llvm.minnum.f64", - // sym::maxnumf32 => "llvm.maxnum.f32", - // sym::maxnumf64 => "llvm.maxnum.f64", - // sym::copysignf32 => "llvm.copysign.f32", - // sym::copysignf64 => "llvm.copysign.f64", - // sym::floorf32 => "llvm.floor.f32", - // sym::floorf64 => "llvm.floor.f64", - // sym::ceilf32 => "llvm.ceil.f32", - // sym::ceilf64 => "llvm.ceil.f64", - // sym::truncf32 => "llvm.trunc.f32", - // sym::truncf64 => "llvm.trunc.f64", - // sym::rintf32 => "llvm.rint.f32", - // sym::rintf64 => "llvm.rint.f64", - // sym::nearbyintf32 => "llvm.nearbyint.f32", - // sym::nearbyintf64 => "llvm.nearbyint.f64", - // sym::roundf32 => "llvm.round.f32", - // sym::roundf64 => "llvm.round.f64", - // _ => return None, - // }; - // Some(cx.get_intrinsic(&llvm_name)) - - tree builtin = ctx->get_backend ()->lookup_builtin_by_rust_name ( - fntype->get_identifier ()); - if (builtin != nullptr) + tree builtin = error_mark_node; + SimpleIntrinsics &simple_intrinsics = SimpleIntrinsics::get (); + if (simple_intrinsics.lookup_simple_builtin (fntype->get_identifier (), + &builtin)) return builtin; // is it an generic builtin? diff --git a/gcc/rust/rust-backend.h b/gcc/rust/rust-backend.h index b942ed3..6bfebe3 100644 --- a/gcc/rust/rust-backend.h +++ b/gcc/rust/rust-backend.h @@ -504,12 +504,6 @@ public: const std::vector<Bvariable *> ¶m_vars) = 0; - // Look up a named built-in function in the current backend implementation. - // Returns NULL if no built-in function by that name exists. - virtual tree lookup_gcc_builtin (const std::string &) = 0; - - virtual tree lookup_builtin_by_rust_name (const std::string &) = 0; - // Utility. // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS, diff --git a/gcc/rust/rust-gcc.cc b/gcc/rust/rust-gcc.cc index 87affcf..6ea9ef3 100644 --- a/gcc/rust/rust-gcc.cc +++ b/gcc/rust/rust-gcc.cc @@ -344,19 +344,6 @@ private: tree non_zero_size_type (tree); tree convert_tree (tree, tree, Location); - -private: - static const int builtin_const = 1 << 0; - static const int builtin_noreturn = 1 << 1; - static const int builtin_novops = 1 << 2; - - void define_builtin (const std::string rust_name, built_in_function bcode, - const char *name, const char *libname, tree fntype, - int flags); - - // A mapping of the GCC built-ins exposed to GCCRust. - std::map<std::string, tree> builtin_functions_; - std::map<std::string, std::string> rust_intrinsic_to_gcc_builtin; }; // A helper function to create a GCC identifier from a C++ string. @@ -473,14 +460,6 @@ Gcc_backend::Gcc_backend () // builtin_const); // We provide some functions for the math library. - tree math_function_type_f32 - = build_function_type_list (float_type_node, float_type_node, NULL_TREE); - - this->define_builtin ("sinf32", BUILT_IN_SINF, "__builtin_sinf", "sinf", - math_function_type_f32, builtin_const); - - this->define_builtin ("sqrtf32", BUILT_IN_SQRTF, "__builtin_sqrtf", "sqrtf", - math_function_type_f32, builtin_const); // We use __builtin_return_address in the thunk we build for // functions which call recover, and for runtime.getcallerpc. @@ -2661,27 +2640,6 @@ Gcc_backend::function_set_parameters ( return true; } -// Look up a named built-in function in the current backend implementation. -// Returns NULL if no built-in function by that name exists. - -tree -Gcc_backend::lookup_gcc_builtin (const std::string &name) -{ - if (this->builtin_functions_.count (name) != 0) - return this->builtin_functions_[name]; - return NULL; -} - -tree -Gcc_backend::lookup_builtin_by_rust_name (const std::string &name) -{ - auto it = rust_intrinsic_to_gcc_builtin.find (name); - if (it == rust_intrinsic_to_gcc_builtin.end ()) - return NULL; - - return lookup_gcc_builtin (it->second); -} - // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS, // FUNCTION_DECLS, and VARIABLE_DECLS declared globally, as well as // emit early debugging information. @@ -2763,44 +2721,6 @@ Gcc_backend::write_export_data (const char *bytes, unsigned int size) rust_write_export_data (bytes, size); } -// Define a builtin function. BCODE is the builtin function code -// defined by builtins.def. NAME is the name of the builtin function. -// LIBNAME is the name of the corresponding library function, and is -// NULL if there isn't one. FNTYPE is the type of the function. -// CONST_P is true if the function has the const attribute. -// NORETURN_P is true if the function has the noreturn attribute. - -void -Gcc_backend::define_builtin (const std::string rust_name, - built_in_function bcode, const char *name, - const char *libname, tree fntype, int flags) -{ - tree decl = add_builtin_function (name, fntype, bcode, BUILT_IN_NORMAL, - libname, NULL_TREE); - if ((flags & builtin_const) != 0) - TREE_READONLY (decl) = 1; - if ((flags & builtin_noreturn) != 0) - TREE_THIS_VOLATILE (decl) = 1; - if ((flags & builtin_novops) != 0) - DECL_IS_NOVOPS (decl) = 1; - set_builtin_decl (bcode, decl, true); - this->builtin_functions_[name] = decl; - if (libname != NULL) - { - decl = add_builtin_function (libname, fntype, bcode, BUILT_IN_NORMAL, - NULL, NULL_TREE); - if ((flags & builtin_const) != 0) - TREE_READONLY (decl) = 1; - if ((flags & builtin_noreturn) != 0) - TREE_THIS_VOLATILE (decl) = 1; - if ((flags & builtin_novops) != 0) - DECL_IS_NOVOPS (decl) = 1; - this->builtin_functions_[libname] = decl; - } - - rust_intrinsic_to_gcc_builtin[rust_name] = name; -} - // Return the backend generator. Backend * |