-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extend type checking to mark extern function call that returns enum a… (
#4941) * extend type checking to mark extern function call that returns enum as compile time constant Signed-off-by: Wang, Han2 <[email protected]> * use @pure to annotate extern function that should be treated as compiled-time constant update reference output Signed-off-by: Wang, Han2 <[email protected]> * address comment Signed-off-by: Wang, Han2 <[email protected]> --------- Signed-off-by: Wang, Han2 <[email protected]>
- Loading branch information
Showing
7 changed files
with
146 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
Copyright 2024 Intel Corp. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
enum HashAlgorithm_t { | ||
IDENTITY, | ||
RANDOM | ||
} | ||
|
||
@pure extern HashAlgorithm_t identity_hash(bool msb, bool extend); | ||
@pure extern HashAlgorithm_t random_hash(bool msb, bool extend); | ||
|
||
extern Hash<W> { | ||
/// Constructor | ||
/// @type_param W : width of the calculated hash. | ||
/// @param algo : The default algorithm used for hash calculation. | ||
Hash(HashAlgorithm_t algo); | ||
|
||
/// Compute the hash for the given data. | ||
/// @param data : The list of fields contributing to the hash. | ||
/// @return The hash value. | ||
W get<D>(in D data); | ||
} | ||
|
||
header h1_t { | ||
bit<32> f1; | ||
bit<32> f2; | ||
bit<32> f3; | ||
} | ||
|
||
struct hdrs { | ||
h1_t h1; | ||
bit<16> hash; | ||
} | ||
|
||
control test(inout hdrs hdr) { | ||
apply { | ||
hdr.hash = Hash<bit<16>>(random_hash(false, false)).get(hdr.h1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
enum HashAlgorithm_t { | ||
IDENTITY, | ||
RANDOM | ||
} | ||
|
||
@pure extern HashAlgorithm_t identity_hash(bool msb, bool extend); | ||
@pure extern HashAlgorithm_t random_hash(bool msb, bool extend); | ||
extern Hash<W> { | ||
Hash(HashAlgorithm_t algo); | ||
W get<D>(in D data); | ||
} | ||
|
||
header h1_t { | ||
bit<32> f1; | ||
bit<32> f2; | ||
bit<32> f3; | ||
} | ||
|
||
struct hdrs { | ||
h1_t h1; | ||
bit<16> hash; | ||
} | ||
|
||
control test(inout hdrs hdr) { | ||
apply { | ||
hdr.hash = Hash<bit<16>>(random_hash(false, false)).get<h1_t>(hdr.h1); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
enum HashAlgorithm_t { | ||
IDENTITY, | ||
RANDOM | ||
} | ||
|
||
@pure extern HashAlgorithm_t identity_hash(bool msb, bool extend); | ||
@pure extern HashAlgorithm_t random_hash(bool msb, bool extend); | ||
extern Hash<W> { | ||
Hash(HashAlgorithm_t algo); | ||
W get<D>(in D data); | ||
} | ||
|
||
header h1_t { | ||
bit<32> f1; | ||
bit<32> f2; | ||
bit<32> f3; | ||
} | ||
|
||
struct hdrs { | ||
h1_t h1; | ||
bit<16> hash; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
enum HashAlgorithm_t { | ||
IDENTITY, | ||
RANDOM | ||
} | ||
|
||
@pure extern HashAlgorithm_t identity_hash(bool msb, bool extend); | ||
@pure extern HashAlgorithm_t random_hash(bool msb, bool extend); | ||
extern Hash<W> { | ||
Hash(HashAlgorithm_t algo); | ||
W get<D>(in D data); | ||
} | ||
|
||
header h1_t { | ||
bit<32> f1; | ||
bit<32> f2; | ||
bit<32> f3; | ||
} | ||
|
||
struct hdrs { | ||
h1_t h1; | ||
bit<16> hash; | ||
} | ||
|
||
control test(inout hdrs hdr) { | ||
apply { | ||
hdr.hash = Hash<bit<16>>(random_hash(false, false)).get(hdr.h1); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
hashext3.p4(48): [--Wwarn=unused] warning: Control test is not used; removing | ||
control test(inout hdrs hdr) { | ||
^^^^ | ||
[--Wwarn=missing] warning: Program does not contain a `main' module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
issue4661_pure_extern_function_const_args.p4(13): [--Wwarn=mismatch] warning: baz(): constant expression in switch | ||
switch(baz()) { | ||
^^^^^ |