[llvm] [WebAssembly] Use SetVector instead of SmallPtrSet in FixBrTableDefaults (PR #84418)

Derek Schuff via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 7 18:10:33 PST 2024


https://github.com/dschuff created https://github.com/llvm/llvm-project/pull/84418

This pass inserts all the MBBs into a set and then iterates over them. But when
the number of elements gets large enough, SmallPtrSet expands into a hash table
which wouldn't have a deterministic iteration order since the elements are
pointers. This results in nondeterministic jump table layouts.
Use SetVector instead for a deterministic iteration order.

>From 0d28e32ce1940e20b908877784e0b660903e3d43 Mon Sep 17 00:00:00 2001
From: Derek Schuff <dschuff at chromium.org>
Date: Thu, 7 Mar 2024 18:03:03 -0800
Subject: [PATCH] [WebAssembly] Use SetVector instead of SmallPtrSet in
 FixBrTableDefaults

This pass inserts all the MBBs into a set and then iterates over them. But when
the number of elements gets large enough, SmallPtrSet expands into a hash table
which wouldn't have a deterministic iteration order since the elements are
pointers. This results in nondeterministic jump table layouts.
Use SetVector instead for a deterministic iteration order.
---
 .../Target/WebAssembly/WebAssemblyFixBrTableDefaults.cpp  | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyFixBrTableDefaults.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyFixBrTableDefaults.cpp
index 495f19a7ccde55..4252fc1f55fc9f 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyFixBrTableDefaults.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyFixBrTableDefaults.cpp
@@ -159,19 +159,21 @@ bool WebAssemblyFixBrTableDefaults::runOnMachineFunction(MachineFunction &MF) {
                     << MF.getName() << '\n');
 
   bool Changed = false;
-  SmallPtrSet<MachineBasicBlock *, 16> MBBSet;
+  SetVector<MachineBasicBlock *, SmallVector<MachineBasicBlock *, 16>,
+            DenseSet<MachineBasicBlock *>, 16>
+      MBBSet;
   for (auto &MBB : MF)
     MBBSet.insert(&MBB);
 
   while (!MBBSet.empty()) {
     MachineBasicBlock *MBB = *MBBSet.begin();
-    MBBSet.erase(MBB);
+    MBBSet.remove(MBB);
     for (auto &MI : *MBB) {
       if (WebAssembly::isBrTable(MI.getOpcode())) {
         fixBrTableIndex(MI, MBB, MF);
         auto *Fixed = fixBrTableDefault(MI, MBB, MF);
         if (Fixed != nullptr) {
-          MBBSet.erase(Fixed);
+          MBBSet.remove(Fixed);
           Changed = true;
         }
         break;



More information about the llvm-commits mailing list