[llvm] [DirectX] Add support to lower LLVM intrinsics ceil, cos, fabs, floor and smax to DXIL Ops. (PR #78767)

David Peixotto via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 23 16:46:18 PST 2024


================
@@ -0,0 +1,135 @@
+//===- DXILStrengthReduce.cpp - Prepare LLVM Module for DXIL encoding------===//
+//
+// 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 contains strength reduction pass to convert a modern LLVM
+/// module into an LLVM module with LLVM intrinsics amenable for lowering to
+/// LLVM 3.7-based DirectX Intermediate Language (DXIL).
+//===----------------------------------------------------------------------===//
+
+#include "DirectX.h"
+#include "DirectXIRPasses/PointerTypeAnalysis.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/CodeGen/Passes.h"
+#include "llvm/IR/AttributeMask.h"
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/Instruction.h"
+#include "llvm/IR/Module.h"
+#include "llvm/InitializePasses.h"
+#include "llvm/Pass.h"
+#include "llvm/Support/Compiler.h"
+
+#define DEBUG_TYPE "dxil-strength-reduce"
+
+using namespace llvm;
+using namespace llvm::dxil;
+
+namespace {
+class DXILStrengthReduce : public ModulePass {
+
+public:
+  bool runOnModule(Module &M) override {
+    for (auto &F : make_early_inc_range(M.functions())) {
+      IRBuilder<> IRB(M.getContext());
+      // Reduce strength of LLVM intrinsics
+      // Flag to indicate if the intrinsic has been replaced. This ensures any
+      // other functions with no uses are not deleted in this pass.
+      bool IntrinsicReplaced = false;
+      if (F.isDeclaration()) {
----------------
dmpots wrote:

I find big chunks of inlined code hard to read. If we expect this pass to be extended in the future I think it is worth separating out some functions to make the overall logic easier to follow like

```
void strengthReduceAbs(Function &F) {
    // Strength reduce abs to max.
}

void strengthReduceFunctionDecl(Function &F) {
    switch (F.getIntrinsicID()) {
        case Intrinsic::abs: strengthReduceAbs(F); break;
    }
    // Handle FNeg for now.
}

void strengthReduceFunctionBody(Function &F)
{
    // Handle FNeg for now.
}


runOnModule() {
    for (auto &F : make_early_inc_range(M.functions())) {
        if (F.isDeclaration())
            strengthReduceFunctionDecl(F);
        else
            strenghtReduceFunctionBody(F)
        }
}
```

https://github.com/llvm/llvm-project/pull/78767


More information about the llvm-commits mailing list