forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WebAssembly] remove instruction after builtin trap (llvm#90207)
`llvm.trap` will be convert as `unreachable` which is terminator. Instruction after terminator will cause validation failed. This PR introduces a pass to clean instruction after terminator. Fixes: llvm#68770.
- Loading branch information
1 parent
7b5b521
commit ff03f23
Showing
6 changed files
with
102 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
llvm/lib/Target/WebAssembly/WebAssemblyCleanCodeAfterTrap.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
//===-- WebAssemblyCleanCodeAfterTrap.cpp - Clean Code After Trap ---------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// \file | ||
/// This file remove instruction after trap. | ||
/// ``llvm.trap`` will be convert as ``unreachable`` which is terminator. | ||
/// Instruction after terminator will cause validation failed. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "WebAssembly.h" | ||
#include "WebAssemblyUtilities.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h" | ||
#include "llvm/CodeGen/Passes.h" | ||
#include "llvm/MC/MCInstrDesc.h" | ||
#include "llvm/Support/Debug.h" | ||
#include "llvm/Support/raw_ostream.h" | ||
using namespace llvm; | ||
|
||
#define DEBUG_TYPE "wasm-clean-code-after-trap" | ||
|
||
namespace { | ||
class WebAssemblyCleanCodeAfterTrap final : public MachineFunctionPass { | ||
public: | ||
static char ID; // Pass identification, replacement for typeid | ||
WebAssemblyCleanCodeAfterTrap() : MachineFunctionPass(ID) {} | ||
|
||
StringRef getPassName() const override { | ||
return "WebAssembly Clean Code After Trap"; | ||
} | ||
|
||
bool runOnMachineFunction(MachineFunction &MF) override; | ||
}; | ||
} // end anonymous namespace | ||
|
||
char WebAssemblyCleanCodeAfterTrap::ID = 0; | ||
INITIALIZE_PASS(WebAssemblyCleanCodeAfterTrap, DEBUG_TYPE, | ||
"WebAssembly Clean Code After Trap", false, false) | ||
|
||
FunctionPass *llvm::createWebAssemblyCleanCodeAfterTrap() { | ||
return new WebAssemblyCleanCodeAfterTrap(); | ||
} | ||
|
||
bool WebAssemblyCleanCodeAfterTrap::runOnMachineFunction(MachineFunction &MF) { | ||
LLVM_DEBUG({ | ||
dbgs() << "********** CleanCodeAfterTrap **********\n" | ||
<< "********** Function: " << MF.getName() << '\n'; | ||
}); | ||
|
||
bool Changed = false; | ||
|
||
for (MachineBasicBlock &BB : MF) { | ||
bool HasTerminator = false; | ||
llvm::SmallVector<MachineInstr *> RemoveMI{}; | ||
for (MachineInstr &MI : BB) { | ||
if (HasTerminator) | ||
RemoveMI.push_back(&MI); | ||
if (MI.hasProperty(MCID::Trap) && MI.isTerminator()) | ||
HasTerminator = true; | ||
} | ||
if (!RemoveMI.empty()) { | ||
Changed = true; | ||
LLVM_DEBUG({ | ||
for (MachineInstr *MI : RemoveMI) { | ||
llvm::dbgs() << "* remove "; | ||
MI->print(llvm::dbgs()); | ||
} | ||
}); | ||
for (MachineInstr *MI : RemoveMI) | ||
MI->eraseFromParent(); | ||
} | ||
} | ||
return Changed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters