1f319bbbdSRiver Riddle //===- StripDebugInfo.cpp - Pass to strip debug information ---------------===//
2f319bbbdSRiver Riddle //
330857107SMehdi Amini // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
456222a06SMehdi Amini // See https://llvm.org/LICENSE.txt for license information.
556222a06SMehdi Amini // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f319bbbdSRiver Riddle //
756222a06SMehdi Amini //===----------------------------------------------------------------------===//
8f319bbbdSRiver Riddle 
91834ad4aSRiver Riddle #include "PassDetail.h"
1065fcddffSRiver Riddle #include "mlir/IR/BuiltinOps.h"
119ffdc930SRiver Riddle #include "mlir/IR/Operation.h"
1248ccae24SRiver Riddle #include "mlir/Pass/Pass.h"
13f319bbbdSRiver Riddle #include "mlir/Transforms/Passes.h"
14f319bbbdSRiver Riddle 
15f319bbbdSRiver Riddle using namespace mlir;
16f319bbbdSRiver Riddle 
17f319bbbdSRiver Riddle namespace {
181834ad4aSRiver Riddle struct StripDebugInfo : public StripDebugInfoBase<StripDebugInfo> {
19c33d6970SRiver Riddle   void runOnOperation() override;
20f319bbbdSRiver Riddle };
21*be0a7e9fSMehdi Amini } // namespace
22f319bbbdSRiver Riddle 
runOnOperation()23c33d6970SRiver Riddle void StripDebugInfo::runOnOperation() {
2449162524SRiver Riddle   auto unknownLoc = UnknownLoc::get(&getContext());
25e5eff533Sthomasraoux 
26e5eff533Sthomasraoux   // Strip the debug info from all operations.
27e5eff533Sthomasraoux   getOperation()->walk([&](Operation *op) {
28e5eff533Sthomasraoux     op->setLoc(unknownLoc);
29e5eff533Sthomasraoux     // Strip block arguments debug info.
30e5eff533Sthomasraoux     for (Region &region : op->getRegions()) {
31e5eff533Sthomasraoux       for (Block &block : region.getBlocks()) {
32e5eff533Sthomasraoux         for (BlockArgument &arg : block.getArguments()) {
33e5eff533Sthomasraoux           arg.setLoc(unknownLoc);
34e5eff533Sthomasraoux         }
35e5eff533Sthomasraoux       }
36e5eff533Sthomasraoux     }
37e5eff533Sthomasraoux   });
38f319bbbdSRiver Riddle }
39f319bbbdSRiver Riddle 
40f319bbbdSRiver Riddle /// Creates a pass to strip debug information from a function.
createStripDebugInfoPass()41c33d6970SRiver Riddle std::unique_ptr<Pass> mlir::createStripDebugInfoPass() {
4279f53b0cSJacques Pienaar   return std::make_unique<StripDebugInfo>();
43c6c53449SRiver Riddle }
44