public inbox for gcc-rust@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Add support for const bool and const float
@ 2021-08-15 19:55 Mark Wielaard
  2021-08-16  7:48 ` dkm
  0 siblings, 1 reply; 5+ messages in thread
From: Mark Wielaard @ 2021-08-15 19:55 UTC (permalink / raw)
  To: gcc-rust; +Cc: Mark Wielaard

Handle BOOL and FLOAT in ConstFoldExpr::visit (HIR::LiteralExpr) to
make it possible to create const bool, f32 and f64 constants. Add a
new testcase "primconsts.rs". Not yet handled are const char and &str
types.
---
 gcc/rust/typecheck/rust-hir-const-fold.h      | 30 ++++++++
 .../rust/compile/torture/primconsts.rs        | 72 +++++++++++++++++++
 2 files changed, 102 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/torture/primconsts.rs

diff --git a/gcc/rust/typecheck/rust-hir-const-fold.h b/gcc/rust/typecheck/rust-hir-const-fold.h
index f6c66163fc1..8efbb183403 100644
--- a/gcc/rust/typecheck/rust-hir-const-fold.h
+++ b/gcc/rust/typecheck/rust-hir-const-fold.h
@@ -315,6 +315,36 @@ public:
 	}
 	return;
 
+	case HIR::Literal::BOOL: {
+	  bool bval = literal_value->as_string ().compare ("true") == 0;
+	  folded = ctx->get_backend ()->boolean_constant_expression (bval);
+	}
+	return;
+
+	case HIR::Literal::FLOAT: {
+	  mpfr_t fval;
+	  if (mpfr_init_set_str (fval, literal_value->as_string ().c_str (), 10,
+				 MPFR_RNDN)
+	      != 0)
+	    {
+	      rust_fatal_error (expr.get_locus (),
+				"bad floating-point number in literal");
+	      return;
+	    }
+
+	  TyTy::BaseType *tyty = nullptr;
+	  if (!tyctx->lookup_type (expr.get_mappings ().get_hirid (), &tyty))
+	    {
+	      rust_fatal_error (expr.get_locus (),
+				"did not resolve type for this literal expr");
+	      return;
+	    }
+
+	  Btype *type = ConstFoldType::fold (tyty, ctx->get_backend ());
+	  folded = ctx->get_backend ()->float_constant_expression (type, fval);
+	}
+	return;
+
 	/* handle other literals */
 
       default:
diff --git a/gcc/testsuite/rust/compile/torture/primconsts.rs b/gcc/testsuite/rust/compile/torture/primconsts.rs
new file mode 100644
index 00000000000..bcf9456d059
--- /dev/null
+++ b/gcc/testsuite/rust/compile/torture/primconsts.rs
@@ -0,0 +1,72 @@
+const TRUE: bool = true;
+const FALSE: bool = !TRUE;
+
+const U8ZERO: u8 = 0;
+const U8ONE: u8 = U8ZERO + 1;
+const U16ZERO: u16 = 0;
+const U16ONE: u16 = U16ZERO + 1;
+const U32ZERO: u32 = 0;
+const U32ONE: u32 = U32ZERO + 1;
+const U64ZERO: u64 = 0;
+const U64ONE: u64 = U64ZERO + 1;
+const U128ZERO: u128 = 0;
+const U128ONE: u128 = U128ZERO + 1;
+
+const I8ZERO: i8 = 0;
+const I8ONE: i8 = I8ZERO + 1;
+const I16ZERO: i16 = 0;
+const I16ONE: i16 = I16ZERO + 1;
+const I32ZERO: i32 = 0;
+const I32ONE: i32 = I32ZERO + 1;
+const I64ZERO: i64 = 0;
+const I64ONE: i64 = I64ZERO + 1;
+const I128ZERO: i128 = 0;
+const I128ONE: i128 = I128ZERO + 1;
+
+const F32ZERO: f32 = 0.0;
+const F32ONE: f32 = F32ZERO + 1.0;
+const F64ZERO: f64 = 0.0;
+const F64ONE: f64 = F64ZERO + 1.0;
+
+const USIZEZERO: usize = 0;
+const USIZEONE: usize = USIZEZERO + 1;
+const ISIZEZERO: isize = 0;
+const ISIZEONE: isize = ISIZEZERO + 1;
+
+/* Not yet supported 
+const CHARPI: char = '\u{03C0}';
+const STRHELLO: &str = "Hello World!";
+*/
+
+extern "C" { fn abort (); }
+
+pub fn main ()
+{
+  if TRUE == FALSE { unsafe { abort (); } }
+  if U8ZERO > U8ONE { unsafe { abort (); } }
+  if U16ZERO > U16ONE { unsafe { abort (); } }
+  if U32ZERO > U32ONE { unsafe { abort (); } }
+  if U64ZERO > U64ONE { unsafe { abort (); } }
+  if U128ZERO > U128ONE { unsafe { abort (); } }
+
+  if I8ONE <= I8ZERO { unsafe { abort (); } }
+  if I16ONE <= I16ZERO { unsafe { abort (); } }
+  if I32ONE <= I32ZERO { unsafe { abort (); } }
+  if I64ONE <= I64ZERO { unsafe { abort (); } }
+  if I128ONE <= I128ZERO { unsafe { abort (); } }
+
+  if F32ZERO + F32ONE != F32ONE { unsafe { abort (); } }
+  if F64ZERO + F64ONE != F64ONE { unsafe { abort (); } }
+
+  if USIZEZERO + USIZEONE - USIZEONE + USIZEZERO != USIZEZERO
+    {
+      unsafe { abort (); }
+    }
+  if ISIZEZERO + ISIZEONE - ISIZEONE + ISIZEZERO != ISIZEZERO
+    {
+      unsafe { abort (); }
+    }
+
+ // if CHARPI != '\u{03c0}'  { unsafe { abort (); } }
+ // if STRHELLO != "Hello World!" { unsafe { abort (); } }
+}
-- 
2.32.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Add support for const bool and const float
  2021-08-15 19:55 [PATCH] Add support for const bool and const float Mark Wielaard
@ 2021-08-16  7:48 ` dkm
  2021-08-16  8:52   ` Mark Wielaard
  0 siblings, 1 reply; 5+ messages in thread
From: dkm @ 2021-08-16  7:48 UTC (permalink / raw)
  To: Mark Wielaard, gcc-rust

Hey!

Looks like tests are not OK, at least in the github action. Can't test but maybe you can confirm this issue ?

# of unexpected failures	14

Marc

August 15, 2021 9:55 PM, "Mark Wielaard" <mark@klomp.org> wrote:

> Handle BOOL and FLOAT in ConstFoldExpr::visit (HIR::LiteralExpr) to
> make it possible to create const bool, f32 and f64 constants. Add a
> new testcase "primconsts.rs". Not yet handled are const char and &str
> types.
> ---
> gcc/rust/typecheck/rust-hir-const-fold.h | 30 ++++++++
> .../rust/compile/torture/primconsts.rs | 72 +++++++++++++++++++
> 2 files changed, 102 insertions(+)
> create mode 100644 gcc/testsuite/rust/compile/torture/primconsts.rs
> 
> diff --git a/gcc/rust/typecheck/rust-hir-const-fold.h b/gcc/rust/typecheck/rust-hir-const-fold.h
> index f6c66163fc1..8efbb183403 100644
> --- a/gcc/rust/typecheck/rust-hir-const-fold.h
> +++ b/gcc/rust/typecheck/rust-hir-const-fold.h
> @@ -315,6 +315,36 @@ public:
> }
> return;
> 
> + case HIR::Literal::BOOL: {
> + bool bval = literal_value->as_string ().compare ("true") == 0;
> + folded = ctx->get_backend ()->boolean_constant_expression (bval);
> + }
> + return;
> +
> + case HIR::Literal::FLOAT: {
> + mpfr_t fval;
> + if (mpfr_init_set_str (fval, literal_value->as_string ().c_str (), 10,
> + MPFR_RNDN)
> + != 0)
> + {
> + rust_fatal_error (expr.get_locus (),
> + "bad floating-point number in literal");
> + return;
> + }
> +
> + TyTy::BaseType *tyty = nullptr;
> + if (!tyctx->lookup_type (expr.get_mappings ().get_hirid (), &tyty))
> + {
> + rust_fatal_error (expr.get_locus (),
> + "did not resolve type for this literal expr");
> + return;
> + }
> +
> + Btype *type = ConstFoldType::fold (tyty, ctx->get_backend ());
> + folded = ctx->get_backend ()->float_constant_expression (type, fval);
> + }
> + return;
> +
> /* handle other literals */
> 
> default:
> diff --git a/gcc/testsuite/rust/compile/torture/primconsts.rs
> b/gcc/testsuite/rust/compile/torture/primconsts.rs
> new file mode 100644
> index 00000000000..bcf9456d059
> --- /dev/null
> +++ b/gcc/testsuite/rust/compile/torture/primconsts.rs
> @@ -0,0 +1,72 @@
> +const TRUE: bool = true;
> +const FALSE: bool = !TRUE;
> +
> +const U8ZERO: u8 = 0;
> +const U8ONE: u8 = U8ZERO + 1;
> +const U16ZERO: u16 = 0;
> +const U16ONE: u16 = U16ZERO + 1;
> +const U32ZERO: u32 = 0;
> +const U32ONE: u32 = U32ZERO + 1;
> +const U64ZERO: u64 = 0;
> +const U64ONE: u64 = U64ZERO + 1;
> +const U128ZERO: u128 = 0;
> +const U128ONE: u128 = U128ZERO + 1;
> +
> +const I8ZERO: i8 = 0;
> +const I8ONE: i8 = I8ZERO + 1;
> +const I16ZERO: i16 = 0;
> +const I16ONE: i16 = I16ZERO + 1;
> +const I32ZERO: i32 = 0;
> +const I32ONE: i32 = I32ZERO + 1;
> +const I64ZERO: i64 = 0;
> +const I64ONE: i64 = I64ZERO + 1;
> +const I128ZERO: i128 = 0;
> +const I128ONE: i128 = I128ZERO + 1;
> +
> +const F32ZERO: f32 = 0.0;
> +const F32ONE: f32 = F32ZERO + 1.0;
> +const F64ZERO: f64 = 0.0;
> +const F64ONE: f64 = F64ZERO + 1.0;
> +
> +const USIZEZERO: usize = 0;
> +const USIZEONE: usize = USIZEZERO + 1;
> +const ISIZEZERO: isize = 0;
> +const ISIZEONE: isize = ISIZEZERO + 1;
> +
> +/* Not yet supported 
> +const CHARPI: char = '\u{03C0}';
> +const STRHELLO: &str = "Hello World!";
> +*/
> +
> +extern "C" { fn abort (); }
> +
> +pub fn main ()
> +{
> + if TRUE == FALSE { unsafe { abort (); } }
> + if U8ZERO > U8ONE { unsafe { abort (); } }
> + if U16ZERO > U16ONE { unsafe { abort (); } }
> + if U32ZERO > U32ONE { unsafe { abort (); } }
> + if U64ZERO > U64ONE { unsafe { abort (); } }
> + if U128ZERO > U128ONE { unsafe { abort (); } }
> +
> + if I8ONE <= I8ZERO { unsafe { abort (); } }
> + if I16ONE <= I16ZERO { unsafe { abort (); } }
> + if I32ONE <= I32ZERO { unsafe { abort (); } }
> + if I64ONE <= I64ZERO { unsafe { abort (); } }
> + if I128ONE <= I128ZERO { unsafe { abort (); } }
> +
> + if F32ZERO + F32ONE != F32ONE { unsafe { abort (); } }
> + if F64ZERO + F64ONE != F64ONE { unsafe { abort (); } }
> +
> + if USIZEZERO + USIZEONE - USIZEONE + USIZEZERO != USIZEZERO
> + {
> + unsafe { abort (); }
> + }
> + if ISIZEZERO + ISIZEONE - ISIZEONE + ISIZEZERO != ISIZEZERO
> + {
> + unsafe { abort (); }
> + }
> +
> + // if CHARPI != '\u{03c0}' { unsafe { abort (); } }
> + // if STRHELLO != "Hello World!" { unsafe { abort (); } }
> +}
> -- 
> 2.32.0
> 
> -- 
> Gcc-rust mailing list
> Gcc-rust@gcc.gnu.org
> https://gcc.gnu.org/mailman/listinfo/gcc-rust

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Add support for const bool and const float
  2021-08-16  7:48 ` dkm
@ 2021-08-16  8:52   ` Mark Wielaard
  2021-08-16 21:03     ` Mark Wielaard
  0 siblings, 1 reply; 5+ messages in thread
From: Mark Wielaard @ 2021-08-16  8:52 UTC (permalink / raw)
  To: dkm, gcc-rust

Hi Marc,

On Mon, 2021-08-16 at 07:48 +0000, dkm@kataplop.net wrote:
> Looks like tests are not OK, at least in the github action.

Are you talking about this patch (which I cannot find on github) or
"Use builtin bool instead of creating new bool types for
ComparisonExpr"? On github I can only see that one has a red cross
which says "build-and-check" but doesn't give any more information
except that "Check regressions" failed, but without any logs ("Sign in
for the full log view").

>  Can't test but maybe you can confirm this issue ?
> 
> # of unexpected failures	14

On my setup, Debian arm64 with gcc 10.2, for both patches the newly
added testcase fails without the patch and succeeds with the patch.
I'll try to find some other setup. What is the configuration for the
machine where you see the new unexpected failures (and with which
patch)?

Thanks,

Mark


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Add support for const bool and const float
  2021-08-16  8:52   ` Mark Wielaard
@ 2021-08-16 21:03     ` Mark Wielaard
  2021-08-16 21:44       ` Mark Wielaard
  0 siblings, 1 reply; 5+ messages in thread
From: Mark Wielaard @ 2021-08-16 21:03 UTC (permalink / raw)
  To: dkm, gcc-rust

Hi,

On Mon, Aug 16, 2021 at 10:52:07AM +0200, Mark Wielaard wrote:
> On Mon, 2021-08-16 at 07:48 +0000, dkm@kataplop.net wrote:
> > Looks like tests are not OK, at least in the github action.
> 
> Are you talking about this patch (which I cannot find on github) or
> "Use builtin bool instead of creating new bool types for
> ComparisonExpr"? On github I can only see that one has a red cross
> which says "build-and-check" but doesn't give any more information
> except that "Check regressions" failed, but without any logs ("Sign in
> for the full log view").
> 
> >  Can't test but maybe you can confirm this issue ?
> > 
> > # of unexpected failures	14
> 
> On my setup, Debian arm64 with gcc 10.2, for both patches the newly
> added testcase fails without the patch and succeeds with the patch.
> I'll try to find some other setup. What is the configuration for the
> machine where you see the new unexpected failures (and with which
> patch)?

On irc we tracked down the confusion. This is about the other patch
"Use builtin bool instead of creating new bool types for
ComparisonExpr".  But rebased on top of current master. Which includes
the testcase rust/compile/torture/ifunaryexpr.rs

So the above failures where:

FAIL: rust/compile/torture/ifunaryexpr.rs   -O0  (internal compiler error)
FAIL: rust/compile/torture/ifunaryexpr.rs   -O0  (test for excess errors)
FAIL: rust/compile/torture/ifunaryexpr.rs   -O1  (internal compiler error)
FAIL: rust/compile/torture/ifunaryexpr.rs   -O1  (test for excess errors)
FAIL: rust/compile/torture/ifunaryexpr.rs   -O2  (internal compiler error)
FAIL: rust/compile/torture/ifunaryexpr.rs   -O2  (test for excess errors)
FAIL: rust/compile/torture/ifunaryexpr.rs   -O3 -g  (internal compiler error)
FAIL: rust/compile/torture/ifunaryexpr.rs   -O3 -g  (test for excess errors)
FAIL: rust/compile/torture/ifunaryexpr.rs   -Os  (internal compiler error)
FAIL: rust/compile/torture/ifunaryexpr.rs   -Os  (test for excess errors)
FAIL: rust/compile/torture/ifunaryexpr.rs   -O2 -flto -fno-use-linker-plugin -flto-partition=none  (internal compiler error)
FAIL: rust/compile/torture/ifunaryexpr.rs   -O2 -flto -fno-use-linker-plugin -flto-partition=none  (test for excess errors)
FAIL: rust/compile/torture/ifunaryexpr.rs   -O2 -flto -fuse-linker-plugin -fno-fat-lto-objects  (internal compiler error)
FAIL: rust/compile/torture/ifunaryexpr.rs   -O2 -flto -fuse-linker-plugin -fno-fat-lto-objects  (test for excess errors)

Now having replicated it locally the rust.log shows:

/srv/gccrs/gccrs/gcc/testsuite/rust/compile/torture/ifunaryexpr.rs: In function 'main':
/srv/gccrs/gccrs/gcc/testsuite/rust/compile/torture/ifunaryexpr.rs:8:5: error: mismatching comparison operand types
const i32
bool
if (n != 0) goto <D.205>; else goto <D.206>;
/srv/gccrs/gccrs/gcc/testsuite/rust/compile/torture/ifunaryexpr.rs:8:5: internal compiler error: 'verify
_gimple' failed
0xf38fbd verify_gimple_in_seq(gimple*)
        ../../gccrs/gcc/tree-cfg.c:5157
0xc72346 gimplify_body(tree_node*, bool)
        ../../gccrs/gcc/gimplify.c:15401
0xc724cd gimplify_function_tree(tree_node*)
        ../../gccrs/gcc/gimplify.c:15472
0xab0dc7 cgraph_node::analyze()
        ../../gccrs/gcc/cgraphunit.c:670
0xab38b7 analyze_functions
        ../../gccrs/gcc/cgraphunit.c:1236
0xab454d symbol_table::finalize_compilation_unit()
        ../../gccrs/gcc/cgraphunit.c:2514

So, my patch created bad gimple. I'll try to track it down.

Cheers,

Mark


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Add support for const bool and const float
  2021-08-16 21:03     ` Mark Wielaard
@ 2021-08-16 21:44       ` Mark Wielaard
  0 siblings, 0 replies; 5+ messages in thread
From: Mark Wielaard @ 2021-08-16 21:44 UTC (permalink / raw)
  To: dkm, gcc-rust

[-- Attachment #1: Type: text/plain, Size: 1396 bytes --]

Hi,

On Mon, Aug 16, 2021 at 11:03:19PM +0200, Mark Wielaard wrote:
> Now having replicated it locally the rust.log shows:
> 
> /srv/gccrs/gccrs/gcc/testsuite/rust/compile/torture/ifunaryexpr.rs: In function 'main':
> /srv/gccrs/gccrs/gcc/testsuite/rust/compile/torture/ifunaryexpr.rs:8:5: error: mismatching comparison operand types
> const i32
> bool
> if (n != 0) goto <D.205>; else goto <D.206>;
> /srv/gccrs/gccrs/gcc/testsuite/rust/compile/torture/ifunaryexpr.rs:8:5: internal compiler error: 'verify
> _gimple' failed
> 0xf38fbd verify_gimple_in_seq(gimple*)
>         ../../gccrs/gcc/tree-cfg.c:5157
> 0xc72346 gimplify_body(tree_node*, bool)
>         ../../gccrs/gcc/gimplify.c:15401
> 0xc724cd gimplify_function_tree(tree_node*)
>         ../../gccrs/gcc/gimplify.c:15472
> 0xab0dc7 cgraph_node::analyze()
>         ../../gccrs/gcc/cgraphunit.c:670
> 0xab38b7 analyze_functions
>         ../../gccrs/gcc/cgraphunit.c:1236
> 0xab454d symbol_table::finalize_compilation_unit()
>         ../../gccrs/gcc/cgraphunit.c:2514
> 
> So, my patch created bad gimple. I'll try to track it down.

I figured it out. There was another bug in the ComparisonExpr type
checker the result is a bool type but the argument types only need to
have compatible types, they don't have to be bools.

Fixed patch attached. Also on
https://code.wildebeest.org/git/user/mjw/gccrs/commit/?h=bools_eq

Cheers,

Mark

[-- Attachment #2: 0001-Use-builtin-bool-instead-of-creating-new-bool-types-.patch --]
[-- Type: text/x-diff, Size: 2217 bytes --]

From 5b229ddbf41c9e74fcce930c26101c1d34a5c9d1 Mon Sep 17 00:00:00 2001
From: Mark Wielaard <mark@klomp.org>
Date: Sat, 14 Aug 2021 23:38:11 +0200
Subject: [PATCH] Use builtin bool instead of creating new bool types for
 ComparisonExpr

The TypeCheckExpr creates a new TyTy::BoolType for a
ComparisonExpr. This new BoolType is unknown to TyTyResolveCompile
which causes a crash when trying to compile the inferred new
BoolType. Resolve this by looking up the builtin bool type.
The new "bools_eq.rs" testcase uses several bools which show
this issue.

Also the lhs and rhs types need to be compatible, but don't
need to be bool type themselves. So don't append the reference
to the inferred type. The existing "ifunaryexpr.rs" testcase
will fail without this fix.
---
 gcc/rust/typecheck/rust-hir-type-check-expr.h  |  6 ++----
 gcc/testsuite/rust/compile/torture/bools_eq.rs | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/torture/bools_eq.rs

diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h
index d88cb0b7f1d..a833822e9b3 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h
@@ -630,10 +630,8 @@ public:
     if (result == nullptr || result->get_kind () == TyTy::TypeKind::ERROR)
       return;
 
-    // we expect this to be
-    infered = new TyTy::BoolType (expr.get_mappings ().get_hirid ());
-    infered->append_reference (lhs->get_ref ());
-    infered->append_reference (rhs->get_ref ());
+    bool ok = context->lookup_builtin ("bool", &infered);
+    rust_assert (ok);
   }
 
   void visit (HIR::LazyBooleanExpr &expr) override
diff --git a/gcc/testsuite/rust/compile/torture/bools_eq.rs b/gcc/testsuite/rust/compile/torture/bools_eq.rs
new file mode 100644
index 00000000000..965127b5d54
--- /dev/null
+++ b/gcc/testsuite/rust/compile/torture/bools_eq.rs
@@ -0,0 +1,18 @@
+extern "C"
+{
+  fn abort ();
+}
+
+fn beq (a: bool, b: bool) -> bool
+{
+  let bools_eq = a == b;
+  bools_eq
+}
+
+pub fn main ()
+{
+  let a = true;
+  let b = false;
+  let r = beq (a, b);
+  if r { unsafe { abort (); } }
+}
-- 
2.32.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-08-16 21:44 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-15 19:55 [PATCH] Add support for const bool and const float Mark Wielaard
2021-08-16  7:48 ` dkm
2021-08-16  8:52   ` Mark Wielaard
2021-08-16 21:03     ` Mark Wielaard
2021-08-16 21:44       ` Mark Wielaard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).