Skip to content

Commit

Permalink
Add delay to FFI annotation.
Browse files Browse the repository at this point in the history
This allows users to specify the delay of an FFI call in the IR. If the user does not provide a delay, they can use the --ffi_fallback_delay_ps flag. However, this user-provided value overrides the value from the --ffi_fallback_delay_ps flag.

PiperOrigin-RevId: 687418386
  • Loading branch information
Balaji V. Iyer authored and copybara-github committed Oct 18, 2024
1 parent 41fe907 commit 84705c8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
7 changes: 7 additions & 0 deletions xls/estimators/delay_model/ffi_delay_estimator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_format.h"
#include "xls/ir/function_base.h"
#include "xls/ir/op.h"

namespace xls {
Expand All @@ -29,6 +30,12 @@ absl::StatusOr<int64_t> FfiDelayEstimator::GetOperationDelayInPs(
absl::StrFormat("FFI delay estimate only for kInvoke, found %s",
OpToString(node->op())));
}
// If the user does not want to provide one, they can use the value from
// the --ffi_fallback_delay_ps flag. However, this user-provided value
// overrides the value from the --ffi_fallback_delay_ps flag.
if (node->function_base()->ForeignFunctionData()->has_delay_ps()) {
return node->function_base()->ForeignFunctionData()->delay_ps();
}
if (!fallback_delay_estimate_.has_value()) {
return absl::NotFoundError("No --ffi_fallback_delay_ps provided.");
}
Expand Down
3 changes: 3 additions & 0 deletions xls/ir/foreign_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ FfiPartialValueSubstituteHelper::GetUpdatedFfiData() const {
CodeTemplate::Escaped::kKeep);
ForeignFunctionData result;
result.set_code_template(modified_template);
if (ffi_->has_delay_ps()) {
result.set_delay_ps(ffi_->delay_ps());
}
return result;
}

Expand Down
3 changes: 2 additions & 1 deletion xls/ir/foreign_function_data.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ package xls;
// the external function instantiation.
message ForeignFunctionData {
string code_template = 1;
// TODO(hzeller): 2023-07-05 Add optional delay model information.
optional int32 delay_ps = 2;
// TODO(hzeller): 2023-07-05 Add optional delay model information?
}
4 changes: 4 additions & 0 deletions xls/ir/foreign_function_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ namespace {
TEST(ForeignFunctionTest, PartialValueSubstituteHelper) {
ForeignFunctionData ffi;
ffi.set_code_template("Some {foo} at {bar} with {baz}");
// Setting some non-zero delay to make sure it is preserved.
ffi.set_delay_ps(1536);
FfiPartialValueSubstituteHelper substitute(ffi);
substitute.SetNamedValue("foo", Value(UBits(0xf00d, 16)));
// '{bar}', we want to keep as-is.
substitute.SetNamedValue("baz", Value(UBits(0xc0ffee, 24)));
EXPECT_EQ(substitute.GetUpdatedFfiData()->code_template(),
"Some 16'hf00d at {bar} with 24'hc0ffee");
EXPECT_EQ(ffi.delay_ps(), 1536);
EXPECT_EQ(substitute.GetUpdatedFfiData()->delay_ps(), 1536);
}
} // namespace
} // namespace xls

0 comments on commit 84705c8

Please sign in to comment.