Skip to content

Commit 1923b7e

Browse files
authored
Add {fmt} (#822)
1 parent c78b8b4 commit 1923b7e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+665
-465
lines changed

cmake/thirdparty/get_fmt.cmake

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#=============================================================================
2+
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
# SPDX-License-Identifier: LicenseRef-NvidiaProprietary
4+
#
5+
# NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
6+
# property and proprietary rights in and to this material, related
7+
# documentation and any modifications thereto. Any use, reproduction,
8+
# disclosure or distribution of this material and related documentation
9+
# without an express license agreement from NVIDIA CORPORATION or
10+
# its affiliates is strictly prohibited.
11+
#=============================================================================
12+
13+
include_guard(GLOBAL)
14+
15+
function(find_or_configure_fmt)
16+
list(APPEND CMAKE_MESSAGE_CONTEXT "fmt")
17+
18+
include(${rapids-cmake-dir}/cpm/fmt.cmake)
19+
20+
rapids_cpm_fmt(BUILD_EXPORT_SET legate-core-exports
21+
INSTALL_EXPORT_SET legate-core-exports)
22+
endfunction()

legate/CMakeLists.txt

+15-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ endif()
2121
# the day it will be an arms race we cannot win.
2222
add_compile_options(-w)
2323

24+
include(../cmake/Modules/find_or_configure.cmake)
25+
26+
legate_core_find_or_configure(PACKAGE fmt)
27+
2428
function(legate_core_create_cython_modules)
2529
list(APPEND CMAKE_MESSAGE_CONTEXT "create_cython_modules")
2630

@@ -36,7 +40,17 @@ function(legate_core_create_cython_modules)
3640

3741
rapids_cython_create_modules(CXX ASSOCIATED_TARGETS legate::core
3842
SOURCE_FILES "${_LEGATE_CORE_CYTHON_SOURCES}"
39-
LINKED_LIBRARIES legate::core Python3::Module)
43+
LINKED_LIBRARIES legate::core
44+
Python3::Module
45+
# Some Cython modules require delving into
46+
# the private headers of legate.core,
47+
# which may contain fmt headers. As a
48+
# result, we also need to add the fmt
49+
# include paths etc. This is not done
50+
# automatically for us, since fmt is a
51+
# PRIVATE dependency of legate.core (as it
52+
# is not found in any public includes).
53+
fmt::fmt)
4054

4155
foreach(target IN LISTS RAPIDS_CYTHON_CREATED_TARGETS)
4256
target_compile_features(${target} PRIVATE cxx_std_${CMAKE_CXX_STANDARD})

legate_core_cpp.cmake

+7-1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ legate_core_find_or_configure(PACKAGE span)
157157

158158
legate_core_find_or_configure(PACKAGE mdspan)
159159

160+
# ########################################################################################
161+
# * fmt::fmt --------------------------------------------------------------
162+
163+
legate_core_find_or_configure(PACKAGE fmt)
164+
160165
# ########################################################################################
161166
# * legate.core --------------------------------------------------------------
162167

@@ -263,6 +268,7 @@ list(APPEND
263268
src/core/utilities/detail/buffer_builder.cc
264269
src/core/utilities/detail/tuple.cc
265270
src/core/utilities/detail/deserializer.cc
271+
src/core/utilities/detail/formatters.cc
266272
src/timing/timing.cc
267273
# stl
268274
src/core/experimental/stl/detail/clang_tidy_dummy.cpp)
@@ -395,7 +401,7 @@ target_link_libraries(legate_core
395401
$<TARGET_NAME_IF_EXISTS:OpenMP::OpenMP_CXX>
396402
$<TARGET_NAME_IF_EXISTS:std::mdspan>
397403
$<TARGET_NAME_IF_EXISTS:std::span>
398-
PRIVATE $<TARGET_NAME_IF_EXISTS:NCCL::NCCL>
404+
PRIVATE $<TARGET_NAME_IF_EXISTS:NCCL::NCCL> fmt::fmt
399405
$<TARGET_NAME_IF_EXISTS:conda_env>)
400406

401407
if(Legion_USE_CUDA)

src/core/data/detail/logical_array.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "core/partitioning/detail/constraint_solver.h"
2020

2121
#include <algorithm>
22+
#include <fmt/format.h>
2223
#include <iterator>
2324
#include <stdexcept>
2425

@@ -240,7 +241,7 @@ InternalSharedPtr<LogicalArray> ListLogicalArray::child(std::uint32_t index) con
240241
case 0: return descriptor_;
241242
case 1: return vardata_;
242243
default: {
243-
throw std::out_of_range{"List array does not have child " + std::to_string(index)};
244+
throw std::out_of_range{fmt::format("List array does not have child {}", index)};
244245
break;
245246
}
246247
}

src/core/data/detail/logical_store.cc

+41-42
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
#include "legate_defines.h"
3030

3131
#include <algorithm>
32+
#include <fmt/format.h>
33+
#include <fmt/ostream.h>
34+
#include <fmt/ranges.h>
3235
#include <numeric>
3336
#include <stdexcept>
3437
#include <utility>
@@ -368,7 +371,7 @@ InternalSharedPtr<StoragePartition> Storage::create_partition(
368371

369372
std::string Storage::to_string() const
370373
{
371-
auto kind_str = [&] {
374+
const auto kind_str = [&] {
372375
switch (kind_) {
373376
case Kind::REGION_FIELD: return "Region";
374377
case Kind::FUTURE: return "Future";
@@ -377,12 +380,12 @@ std::string Storage::to_string() const
377380
LEGATE_UNREACHABLE();
378381
}();
379382

380-
std::stringstream ss;
381-
382-
ss << "Storage(" << storage_id_ << ") {" << shape_->to_string() << ", kind: " << kind_str
383-
<< ", type: " << type_->to_string() << ", level: " << level_ << "}";
384-
385-
return std::move(ss).str();
383+
return fmt::format("Storage({}) {{{}, kind: {}, type: {}, level: {}}}",
384+
storage_id_,
385+
*shape_,
386+
kind_str,
387+
*type_,
388+
level_);
386389
}
387390

388391
////////////////////////////////////////////////////
@@ -445,8 +448,8 @@ namespace {
445448
void assert_fixed_storage_size(const Storage* storage)
446449
{
447450
if (storage->type()->variable_size()) {
448-
throw std::invalid_argument{"Store cannot be created with variable size type " +
449-
storage->type()->to_string()};
451+
throw std::invalid_argument{
452+
fmt::format("Store cannot be created with variable size type {}", *(storage->type()))};
450453
}
451454
}
452455

@@ -510,8 +513,8 @@ void LogicalStore::set_future_map(Legion::FutureMap future_map, std::size_t scal
510513
InternalSharedPtr<LogicalStore> LogicalStore::promote(std::int32_t extra_dim, std::size_t dim_size)
511514
{
512515
if (extra_dim < 0 || extra_dim > static_cast<std::int32_t>(dim())) {
513-
throw std::invalid_argument{"Invalid promotion on dimension " + std::to_string(extra_dim) +
514-
" for a " + std::to_string(dim()) + "-D store"};
516+
throw std::invalid_argument{
517+
fmt::format("Invalid promotion on dimension {} for a {}-D store", extra_dim, dim())};
515518
}
516519

517520
auto new_extents = extents().insert(extra_dim, dim_size);
@@ -524,13 +527,13 @@ InternalSharedPtr<LogicalStore> LogicalStore::project(std::int32_t d, std::int64
524527
auto&& old_extents = extents();
525528

526529
if (d < 0 || d >= static_cast<std::int32_t>(dim())) {
527-
throw std::invalid_argument{"Invalid projection on dimension " + std::to_string(d) + " for a " +
528-
std::to_string(dim()) + "-D store"};
530+
throw std::invalid_argument{
531+
fmt::format("Invalid projection on dimension {} for a {}-D store", d, dim())};
529532
}
530533

531534
if (index < 0 || index >= static_cast<std::int64_t>(old_extents[d])) {
532-
throw std::invalid_argument{"Projection index " + std::to_string(index) +
533-
" is out of bounds [0, " + std::to_string(old_extents[d]) + ")"};
535+
throw std::invalid_argument{
536+
fmt::format("Projection index {} is out of bounds [0, {})", index, old_extents[d])};
534537
}
535538

536539
auto new_extents = old_extents.remove(d);
@@ -550,8 +553,8 @@ InternalSharedPtr<LogicalStore> LogicalStore::slice_(const InternalSharedPtr<Log
550553
{
551554
LEGATE_ASSERT(self.get() == this);
552555
if (dim < 0 || dim >= static_cast<std::int32_t>(this->dim())) {
553-
throw std::invalid_argument{"Invalid slicing of dimension " + std::to_string(dim) + " for a " +
554-
std::to_string(this->dim()) + "-D store"};
556+
throw std::invalid_argument{
557+
fmt::format("Invalid slicing of dimension {} for a {}-D store", dim, this->dim())};
555558
}
556559

557560
constexpr auto sanitize_slice = [](const Slice& san_slice, std::int64_t extent) {
@@ -573,9 +576,8 @@ InternalSharedPtr<LogicalStore> LogicalStore::slice_(const InternalSharedPtr<Log
573576
auto [start, stop] = sanitize_slice(slice, static_cast<std::int64_t>(exts[dim]));
574577

575578
if (start < stop && (start >= exts[dim] || stop > exts[dim])) {
576-
throw std::invalid_argument{"Out-of-bounds slicing on dimension " +
577-
std::to_string(this->dim()) + " for a store of shape " +
578-
extents().to_string()};
579+
throw std::invalid_argument{fmt::format(
580+
"Out-of-bounds slicing on dimension {} for a store of shape {}", this->dim(), extents())};
579581
}
580582

581583
exts[dim] = (start < stop) ? (stop - start) : 0;
@@ -603,8 +605,8 @@ InternalSharedPtr<LogicalStore> LogicalStore::slice_(const InternalSharedPtr<Log
603605
InternalSharedPtr<LogicalStore> LogicalStore::transpose(std::vector<std::int32_t> axes)
604606
{
605607
if (axes.size() != dim()) {
606-
throw std::invalid_argument{"Dimension Mismatch: expected " + std::to_string(dim()) +
607-
" axes, but got " + std::to_string(axes.size())};
608+
throw std::invalid_argument{
609+
fmt::format("Dimension Mismatch: expected {} axes, but got {}", dim(), axes.size())};
608610
}
609611

610612
if (axes.size() != std::set<std::int32_t>{axes.begin(), axes.end()}.size()) {
@@ -613,8 +615,7 @@ InternalSharedPtr<LogicalStore> LogicalStore::transpose(std::vector<std::int32_t
613615

614616
for (auto&& ax_i : axes) {
615617
if (ax_i < 0 || ax_i >= static_cast<std::int32_t>(dim())) {
616-
throw std::invalid_argument{"Invalid axis " + std::to_string(ax_i) + " for a " +
617-
std::to_string(dim()) + "-D store"};
618+
throw std::invalid_argument{fmt::format("Invalid axis {} for a {}-D store", ax_i, dim())};
618619
}
619620
}
620621

@@ -627,8 +628,8 @@ InternalSharedPtr<LogicalStore> LogicalStore::delinearize(std::int32_t dim,
627628
std::vector<std::uint64_t> sizes)
628629
{
629630
if (dim < 0 || dim >= static_cast<std::int32_t>(this->dim())) {
630-
throw std::invalid_argument{"Invalid delinearization on dimension " + std::to_string(dim) +
631-
" for a " + std::to_string(this->dim()) + "-D store"};
631+
throw std::invalid_argument{
632+
fmt::format("Invalid delinearization on dimension {} for a {}-D store", dim, this->dim())};
632633
}
633634

634635
auto&& old_extents = extents();
@@ -642,9 +643,8 @@ InternalSharedPtr<LogicalStore> LogicalStore::delinearize(std::int32_t dim,
642643
};
643644

644645
if (!delinearizable(old_extents[dim], sizes)) {
645-
throw std::invalid_argument{"Dimension of size " + std::to_string(old_extents[dim]) +
646-
" cannot be delinearized into " +
647-
tuple<std::uint64_t>{sizes}.to_string()};
646+
throw std::invalid_argument{
647+
fmt::format("Dimension of size {} cannot be delinearized into {}", old_extents[dim], sizes)};
648648
}
649649

650650
auto new_extents = tuple<std::uint64_t>{};
@@ -669,9 +669,10 @@ InternalSharedPtr<LogicalStorePartition> LogicalStore::partition_by_tiling_(
669669
{
670670
LEGATE_ASSERT(self.get() == this);
671671
if (tile_shape.size() != dim()) {
672-
throw std::invalid_argument{"Incompatible tile shape: expected a " +
673-
std::to_string(extents().size()) + "-tuple, got a " +
674-
std::to_string(tile_shape.size()) + "-tuple"};
672+
throw std::invalid_argument{
673+
fmt::format("Incompatible tile shape: expected a {}-tuple, got a {}-tuple",
674+
extents().size(),
675+
tile_shape.size())};
675676
}
676677
if (tile_shape.volume() == 0) {
677678
throw std::invalid_argument{"Tile shape must have a volume greater than 0"};
@@ -1031,19 +1032,18 @@ std::unique_ptr<Analyzable> LogicalStore::to_launcher_arg_for_fixup_(
10311032

10321033
std::string LogicalStore::to_string() const
10331034
{
1034-
std::stringstream ss;
1035+
auto result = fmt::format("Store({}) {{shape: ", store_id_);
10351036

1036-
ss << "Store(" << store_id_ << ") {shape: ";
10371037
if (unbound()) {
1038-
ss << "(unbound)";
1038+
fmt::format_to(std::back_inserter(result), "(unbound)");
10391039
} else {
1040-
ss << extents();
1040+
fmt::format_to(std::back_inserter(result), "{}", extents());
10411041
}
10421042
if (!transform_->identity()) {
1043-
ss << ", transform: " << *transform_;
1043+
fmt::format_to(std::back_inserter(result), ", transform: {}", fmt::streamed(*transform_));
10441044
}
1045-
ss << ", storage: " << get_storage()->id() << "}";
1046-
return std::move(ss).str();
1045+
fmt::format_to(std::back_inserter(result), ", storage: {}}}", get_storage()->id());
1046+
return result;
10471047
}
10481048

10491049
bool LogicalStore::equal_storage(const LogicalStore& other) const
@@ -1088,9 +1088,8 @@ InternalSharedPtr<LogicalStore> LogicalStorePartition::get_child_store(
10881088
const auto* tiling = static_cast<const Tiling*>(partition_.get());
10891089

10901090
if (!tiling->has_color(color)) {
1091-
throw std::out_of_range{"Color " + color.to_string() +
1092-
" is invalid for partition of color shape " +
1093-
color_shape().to_string()};
1091+
throw std::out_of_range{
1092+
fmt::format("Color {} is invalid for partition of color shape {}", color, color_shape())};
10941093
}
10951094

10961095
auto transform = store_->transform();

src/core/data/detail/physical_array.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "core/data/detail/physical_array.h"
1414

15+
#include <fmt/format.h>
1516
#include <stdexcept>
1617

1718
namespace legate::detail {
@@ -79,7 +80,7 @@ InternalSharedPtr<PhysicalArray> ListPhysicalArray::child(std::uint32_t index) c
7980
case 0: return descriptor();
8081
case 1: return vardata();
8182
default: {
82-
throw std::out_of_range{"List array does not have child " + std::to_string(index)};
83+
throw std::out_of_range{fmt::format("List array does not have child {}", index)};
8384
break;
8485
}
8586
}

src/core/data/detail/physical_store.cc

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
#include "core/utilities/dispatch.h"
1818

1919
#include <cstring> // std::memcpy
20+
#include <fmt/format.h>
2021
#include <stdexcept>
21-
#include <string>
2222

2323
namespace legate::detail {
2424

@@ -129,24 +129,24 @@ void PhysicalStore::bind_empty_data()
129129
void PhysicalStore::check_accessor_dimension_(std::int32_t dim) const
130130
{
131131
if (dim != this->dim() && (this->dim() != 0 || dim != 1)) {
132-
throw std::invalid_argument{"Dimension mismatch: invalid to create a " + std::to_string(dim) +
133-
"-D accessor to a " + std::to_string(this->dim()) + "-D store"};
132+
throw std::invalid_argument{fmt::format(
133+
"Dimension mismatch: invalid to create a {}-D accessor to a {}-D store", dim, this->dim())};
134134
}
135135
}
136136

137137
void PhysicalStore::check_buffer_dimension_(std::int32_t dim) const
138138
{
139139
if (dim != this->dim()) {
140-
throw std::invalid_argument{"Dimension mismatch: invalid to bind a " + std::to_string(dim) +
141-
"-D buffer to a " + std::to_string(this->dim()) + "-D store"};
140+
throw std::invalid_argument{fmt::format(
141+
"Dimension mismatch: invalid to bind a {}-D buffer to a {}-D store", dim, this->dim())};
142142
}
143143
}
144144

145145
void PhysicalStore::check_shape_dimension_(std::int32_t dim) const
146146
{
147147
if (dim != this->dim() && (this->dim() != 0 || dim != 1)) {
148-
throw std::invalid_argument{"Dimension mismatch: invalid to retrieve a " + std::to_string(dim) +
149-
"-D rect from a " + std::to_string(this->dim()) + "-D store"};
148+
throw std::invalid_argument{fmt::format(
149+
"Dimension mismatch: invalid to retrieve a {}-D rect from a {}-D store", dim, this->dim())};
150150
}
151151
}
152152

src/core/data/detail/shape.cc

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "core/runtime/detail/runtime.h"
1717
#include "core/utilities/detail/tuple.h"
1818

19+
#include <fmt/ranges.h>
20+
1921
namespace legate::detail {
2022

2123
Shape::Shape(tuple<std::uint64_t>&& extents)
@@ -76,13 +78,13 @@ std::string Shape::to_string() const
7678
{
7779
switch (state_) {
7880
case State::UNBOUND: {
79-
return "Shape(unbound " + std::to_string(dim_) + "D)";
81+
return fmt::format("Shape(unbound {}D)", dim_);
8082
}
8183
case State::BOUND: {
82-
return "Shape(bound " + std::to_string(dim_) + "D)";
84+
return fmt::format("Shape(bound {}D)", dim_);
8385
}
8486
case State::READY: {
85-
return "Shape" + extents_.to_string();
87+
return fmt::format("Shape {}", extents_);
8688
}
8789
}
8890
return "";

src/core/data/detail/shape.h

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#pragma once
1414

15+
#include "core/utilities/detail/formatters.h"
1516
#include "core/utilities/tuple.h"
1617

1718
#include "legion.h"

0 commit comments

Comments
 (0)