Skip to content

Invalid element reported when return statement is moved #5445

@jhavrane

Description

@jhavrane

Program

Modified p4_16_samples/bfd_offload.p4 program

extern BFD_Offload {
    BFD_Offload(bit<16> size);


    void setTx(in bit<16> index, in bit<8> data);
    bit<8> getTx(in bit<16> index);

    abstract void on_rx( in bit<16> index );
    abstract bool on_tx( in bit<16> index );
}

BFD_Offload(32768) bfd_session_liveness_tracker = {
    void on_rx( in bit<16> index ) {
        this.setTx(index, 0);
    }
    bool on_tx( in bit<16> index ) {
        bit<8> c = this.getTx(index) + 1;
        if (c >= 4) {
            return true;
        } else {
            this.setTx(index, c);
        }
            return false;
    }
};

control for_rx_bfd_packets() {
    apply {
        bit<16> index;
        bfd_session_liveness_tracker.on_rx( index );
    }
}
control for_tx_bfd_packets() {
    apply {
        bit<16> index;
        bfd_session_liveness_tracker.on_tx( index );
    }
}

Diff:

--- p4c/testdata/p4_16_samples/bfd_offload.p4  2025-03-24 09:03:51.864655204 +0000
+++ p4c/testdata/p4_16_samples/issuex.p4       2025-03-22 03:17:33.056023545 +0000
@@ -21,8 +21,8 @@
             return true;
         } else {
             this.setTx(index, c);
-            return false;
         }
+            return false;
     }
 };

Log

p4c/testdata/p4_16_samples/issuex.p4(27): [--Wwarn=unused] warning: control 'for_rx_bfd_packets' is unused
control for_rx_bfd_packets() {
        ^^^^^^^^^^^^^^^^^^
p4c/testdata/p4_16_samples/issuex.p4(33): [--Wwarn=unused] warning: control 'for_tx_bfd_packets' is unused
control for_tx_bfd_packets() {
        ^^^^^^^^^^^^^^^^^^
p4c/testdata/p4_16_samples/issuex.p4(27): [--Winfo=removed] info: removing control 'for_rx_bfd_packets'
control for_rx_bfd_packets() {
        ^^^^^^^^^^^^^^^^^^
p4c/testdata/p4_16_samples/issuex.p4(33): [--Winfo=removed] info: removing control 'for_tx_bfd_packets'
control for_tx_bfd_packets() {
        ^^^^^^^^^^^^^^^^^^
Compiler bug triggered, please report this error:
exception type: P4::Util::CompilerBug
error: In file: ../../p4c/ir/indexed_vector.h:230
Compiler Bug: p4c/testdata/p4_16_samples/issuex.p4(16): invalid element bool on_tx(in bit<16> index) {
  bit<8> tmp
  bit<8> tmp_0
  @name("c") bit<8> c_0/c
  tmp = this.getTx(index);
  tmp_0 = tmp + 1;
  c_0/c = tmp_0;
  if (c_0/c >= 4) {
    return true;
  } else {
    { this.setTx(index, c_0/c);
      return false; } } }
    bool on_tx( in bit<16> index ) {
         ^^^^^

Stack trace:

#0  0x00007ffff3a4e52f in raise () from /usr/lib64/libc.so.6
#1  0x00007ffff3a21e65 in abort () from /usr/lib64/libc.so.6
#2  0x00007ffff42a00f3 in ?? () from libstdc++/libstdc++.so.6
#3  0x00007ffff42b1f71 in std::terminate() () from libstdc++/libstdc++.so.6
#4  0x00007ffff42b21b4 in __cxa_throw () from libstdc++/libstdc++.so.6
#5  0x000000000076a8b5 in P4::IR::IndexedVector<P4::IR::StatOrDecl>::validate() const ()
#6  0x00000000012b861a in P4::Modifier::apply_visitor(P4::IR::Node const*, char const*) ()
#7  0x00000000012c5182 in P4::Visitor::visit(P4::IR::BlockStatement const*&, char const*) ()
#8  0x00000000012b85e7 in P4::Modifier::apply_visitor(P4::IR::Node const*, char const*) ()
#9  0x000000000077c2e9 in P4::IR::Vector<P4::IR::Node>::visit_children(P4::Visitor&, char const*) ()
#10 0x00000000012b85e7 in P4::Modifier::apply_visitor(P4::IR::Node const*, char const*) ()
#11 0x000000000121083f in P4::IR::Node::apply(P4::Visitor&, P4::Visitor_Context const*) const ()
#12 0x0000000001212b66 in P4::PassManager::apply_visitor(P4::IR::Node const*, char const*) ()
#13 0x000000000121083f in P4::IR::Node::apply(P4::Visitor&, P4::Visitor_Context const*) const ()
#14 0x0000000001212b66 in P4::PassManager::apply_visitor(P4::IR::Node const*, char const*) ()
#15 0x00000000012c4902 in P4::Visitor::visit(P4::IR::P4Program const*&, char const*) ()
#16 0x00000000005affd3 in P4::IR::P4Program::apply(P4::Visitor&, P4::Visitor_Context const*) const ()
#17 0x0000000000e9f6d3 in P4::FrontEnd::run(P4::CompilerOptions const&, P4::IR::P4Program const*, std::ostream*) ()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions