[all-commits] [llvm/llvm-project] 3348b8: Make enum iteration with seq safe by default

Jakub Kuderski via All-commits all-commits at lists.llvm.org
Wed Nov 3 17:57:42 PDT 2021


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 3348b841d36e6a189e1bcb30de41b7a8ec8c6991
      https://github.com/llvm/llvm-project/commit/3348b841d36e6a189e1bcb30de41b7a8ec8c6991
  Author: Jakub Kuderski <kubak at google.com>
  Date:   2021-11-03 (Wed, 03 Nov 2021)

  Changed paths:
    M llvm/include/llvm/ADT/Sequence.h
    M llvm/include/llvm/IR/InstrTypes.h
    M llvm/include/llvm/IR/Instructions.h
    M llvm/include/llvm/Support/MachineValueType.h
    M llvm/tools/llvm-exegesis/lib/X86/Target.cpp
    M llvm/unittests/ADT/SequenceTest.cpp
    M llvm/unittests/IR/ConstantRangeTest.cpp

  Log Message:
  -----------
  Make enum iteration with seq safe by default

By default `llvm::seq` would happily iterate over enums, which may be unsafe if the enum values are not continuous. This patch disable enum iteration with `llvm::seq` and `llvm::seq_inclusive` and adds two new functions: `enum_seq` and `enum_seq_inclusive`.

To make sure enum iteration is safe, we require users to declare their enum types as iterable by specializing `enum_iteration_traits<SomeEnum>`. Because it's not always possible to add these traits next to enum definition (e.g., for enums defined in external libraries), we provide an escape hatch to allow iteration on per-callsite basis by passing `force_iteration_on_noniterable_enum`.

The main benefit of this approach is that these global declarations via traits can appear just next to enum definitions, making easy to spot when enums are miss-labeled, e.g., after introducing new enum values, whereas `force_iteration_on_noniterable_enum` should stand out and be easy to grep for.

This emerged from a discussion with gchatelet@ about reusing llvm's `Sequence.h` in lieu of https://github.com/GPUOpen-Drivers/llpc/blob/dev/lgc/interface/lgc/EnumIterator.h.

Reviewed By: dblaikie, gchatelet, aaron.ballman

Differential Revision: https://reviews.llvm.org/D107378




More information about the All-commits mailing list