From c8d2e26facb2ec26f8053dddb5c370e8b81ea264 Mon Sep 17 00:00:00 2001 From: rarbore2 <rarbore2@illinois.edu> Date: Fri, 17 Jan 2025 17:01:49 -0600 Subject: [PATCH] Normalize dynamic constants during typechecking --- hercules_ir/src/typecheck.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/hercules_ir/src/typecheck.rs b/hercules_ir/src/typecheck.rs index 5a64e577..244d8d19 100644 --- a/hercules_ir/src/typecheck.rs +++ b/hercules_ir/src/typecheck.rs @@ -1,3 +1,4 @@ +use std::cmp::{max, min}; use std::collections::HashMap; use std::iter::zip; @@ -1128,7 +1129,8 @@ fn types_match( /* * Determine if the given dynamic constant matches the parameter's dynamic * constants when the provided dynamic constants are substituted in for the - * dynamic constants used in the parameter's dynamic constant + * dynamic constants used in the parameter's dynamic constant. Implement dynamic + * constant normalization here as well - i.e., 1 * 2 * 3 = 6. */ fn dyn_consts_match( dynamic_constants: &Vec<DynamicConstant>, @@ -1136,6 +1138,14 @@ fn dyn_consts_match( param: DynamicConstantID, input: DynamicConstantID, ) -> bool { + // First, try evaluating the DCs and seeing if they're the same value. + if let (Some(cons1), Some(cons2)) = ( + evaluate_dynamic_constant(param, dynamic_constants), + evaluate_dynamic_constant(input, dynamic_constants), + ) { + return cons1 == cons2; + } + match ( &dynamic_constants[param.idx()], &dynamic_constants[input.idx()], @@ -1143,8 +1153,15 @@ fn dyn_consts_match( (DynamicConstant::Constant(x), DynamicConstant::Constant(y)) => x == y, (DynamicConstant::Parameter(i), _) => input == dc_args[*i], (DynamicConstant::Add(pl, pr), DynamicConstant::Add(il, ir)) - | (DynamicConstant::Sub(pl, pr), DynamicConstant::Sub(il, ir)) | (DynamicConstant::Mul(pl, pr), DynamicConstant::Mul(il, ir)) + | (DynamicConstant::Min(pl, pr), DynamicConstant::Min(il, ir)) + | (DynamicConstant::Max(pl, pr), DynamicConstant::Max(il, ir)) => { + // Normalize for associative ops by always looking at smaller DC ID + // as left arm and larger DC ID as right arm. + dyn_consts_match(dynamic_constants, dc_args, min(*pl, *pr), min(*il, *ir)) + && dyn_consts_match(dynamic_constants, dc_args, max(*pl, *pr), max(*il, *ir)) + } + (DynamicConstant::Sub(pl, pr), DynamicConstant::Sub(il, ir)) | (DynamicConstant::Div(pl, pr), DynamicConstant::Div(il, ir)) | (DynamicConstant::Rem(pl, pr), DynamicConstant::Rem(il, ir)) => { dyn_consts_match(dynamic_constants, dc_args, *pl, *il) -- GitLab