|
58 | 58 | #include "xls/dslx/parse_and_typecheck.h"
|
59 | 59 | #include "xls/dslx/type_system/deduce.h"
|
60 | 60 | #include "xls/dslx/type_system/deduce_ctx.h"
|
61 |
| -#include "xls/dslx/type_system/parametric_env.h" |
62 | 61 | #include "xls/dslx/type_system/type.h"
|
63 | 62 | #include "xls/dslx/type_system/type_info.h"
|
64 | 63 | #include "xls/dslx/type_system/typecheck_function.h"
|
|
75 | 74 | namespace xls {
|
76 | 75 | namespace {
|
77 | 76 |
|
78 |
| -absl::StatusOr<bool> IsNegative(const dslx::InterpValue& value) { |
79 |
| - if (!value.IsSigned()) { |
80 |
| - return false; |
81 |
| - } |
82 |
| - XLS_ASSIGN_OR_RETURN(int64_t bit_count, value.GetBitCount()); |
83 |
| - auto zero = dslx::InterpValue::MakeSBits(bit_count, 0); |
84 |
| - XLS_ASSIGN_OR_RETURN(auto lt_zero, value.Lt(zero)); |
85 |
| - XLS_ASSIGN_OR_RETURN(int64_t lt_zero_bit_value, lt_zero.GetBitValueViaSign()); |
86 |
| - return lt_zero_bit_value == 1; |
87 |
| -} |
88 |
| - |
89 | 77 | std::string GetTypeDefName(const dslx::TypeDefinition& type_def) {
|
90 | 78 | return absl::visit(Visitor{
|
91 | 79 | [&](const dslx::ColonRef* n) { return n->ToString(); },
|
@@ -462,56 +450,35 @@ dslx::Unop* DslxBuilder::HandleUnaryOperator(const dslx::Span& span,
|
462 | 450 | }
|
463 | 451 |
|
464 | 452 | absl::StatusOr<dslx::Expr*> DslxBuilder::HandleIntegerExponentiation(
|
465 |
| - const dslx::Span& span, dslx::Expr* lhs, dslx::Expr* rhs) { |
466 |
| - // TODO(b/330575305): 2022-03-16: Switch the model to deduce after |
467 |
| - // constructing a value instead of deducing immediately beforehand. |
468 |
| - XLS_RETURN_IF_ERROR(deduce_ctx().Deduce(lhs).status()); |
469 |
| - XLS_RETURN_IF_ERROR(deduce_ctx().Deduce(rhs).status()); |
470 |
| - XLS_ASSIGN_OR_RETURN(dslx::InterpValue lhs_value, |
471 |
| - InterpretExpr(import_data(), type_info(), lhs)); |
472 |
| - if (!lhs_value.HasBits()) { |
473 |
| - return absl::InvalidArgumentError("pow() LHS isn't bits-typed."); |
474 |
| - } |
475 |
| - |
476 |
| - XLS_ASSIGN_OR_RETURN(dslx::InterpValue rhs_value, |
477 |
| - InterpretExpr(import_data(), type_info(), rhs)); |
478 |
| - if (!rhs_value.HasBits()) { |
479 |
| - return absl::InvalidArgumentError("pow() RHS isn't bits-typed."); |
480 |
| - } |
481 |
| - |
482 |
| - XLS_ASSIGN_OR_RETURN(bool is_negative, IsNegative(rhs_value)); |
483 |
| - if (is_negative) { |
484 |
| - return absl::InvalidArgumentError(absl::StrCat( |
485 |
| - "RHS of pow() cannot be negative: ", rhs_value.ToString())); |
486 |
| - } |
487 |
| - |
| 453 | + const dslx::Span& span, dslx::Expr* lhs, dslx::Expr* rhs, |
| 454 | + verilog::Expression* vast_rhs) { |
| 455 | + XLS_ASSIGN_OR_RETURN(int64_t rhs_value, |
| 456 | + verilog::FoldEntireVastExpr(vast_rhs, vast_type_map_)); |
| 457 | + if (rhs_value < 0) { |
| 458 | + return absl::InvalidArgumentError( |
| 459 | + absl::StrCat("RHS of pow() cannot be negative: ", rhs_value)); |
| 460 | + } |
| 461 | + |
| 462 | + XLS_ASSIGN_OR_RETURN(verilog::DataType * vast_rhs_type, |
| 463 | + GetVastDataType(vast_rhs)); |
| 464 | + XLS_ASSIGN_OR_RETURN(int64_t rhs_bit_count, |
| 465 | + vast_rhs_type->FlatBitCountAsInt64()); |
488 | 466 | auto* element_type = module().Make<dslx::BuiltinTypeAnnotation>(
|
489 | 467 | span, dslx::BuiltinType::kUN,
|
490 | 468 | module().GetOrCreateBuiltinNameDef(dslx::BuiltinType::kUN));
|
491 |
| - XLS_ASSIGN_OR_RETURN(int64_t rhs_bit_count, rhs_value.GetBitCount()); |
492 | 469 | auto* array_dim =
|
493 | 470 | module().Make<dslx::Number>(span, absl::StrCat(rhs_bit_count),
|
494 | 471 | dslx::NumberKind::kOther, /*type=*/nullptr);
|
495 | 472 | auto* new_rhs_type =
|
496 | 473 | module().Make<dslx::ArrayTypeAnnotation>(span, element_type, array_dim);
|
497 | 474 | rhs = module().Make<dslx::Cast>(span, rhs, new_rhs_type);
|
498 | 475 |
|
499 |
| - std::string power_fn = lhs_value.IsUBits() ? "upow" : "spow"; |
500 | 476 | XLS_ASSIGN_OR_RETURN(dslx::Import * std,
|
501 | 477 | GetOrImportModule(dslx::ImportTokens({"std"})));
|
502 | 478 | auto* colon_ref = module().Make<dslx::ColonRef>(
|
503 |
| - span, module().Make<dslx::NameRef>(span, "std", &std->name_def()), |
504 |
| - power_fn); |
| 479 | + span, module().Make<dslx::NameRef>(span, "std", &std->name_def()), "pow"); |
505 | 480 | auto* invocation = module().Make<dslx::Invocation>(
|
506 | 481 | span, colon_ref, std::vector<dslx::Expr*>({lhs, rhs}));
|
507 |
| - dslx::ParametricEnv parametric_env( |
508 |
| - absl::flat_hash_map<std::string, dslx::InterpValue>{ |
509 |
| - {"N", dslx::InterpValue::MakeUBits(32, rhs_bit_count)}}); |
510 |
| - XLS_RETURN_IF_ERROR(type_info().AddInvocationTypeInfo( |
511 |
| - *invocation, /*callee=*/nullptr, /*caller=*/nullptr, |
512 |
| - /*caller_env=*/deduce_ctx().GetCurrentParametricEnv(), |
513 |
| - /*callee_env=*/parametric_env, |
514 |
| - /*derived_type_info=*/nullptr)); |
515 | 482 | return invocation;
|
516 | 483 | }
|
517 | 484 |
|
|
0 commit comments