domada created this revision.
domada added reviewers: dpalermo, skatrak, TIFitis, RogerV-AMD, 
kiranchandramohan, agozillon, kiranktp, NimishMishra.
domada added projects: Flang, OpenMP.
Herald added subscribers: sunshaoce, Moerafaat, zero9178, bzcheeseman, 
awarzynski, sdasgup3, wenzhicui, wrengr, cota, teijeong, rdzhabarov, 
tatianashp, msifontes, jurahul, Kayjukh, grosul1, Joonsoo, liufengdb, aartbik, 
mgester, arpith-jacob, antiagainst, shauheen, rriddle, mehdi_amini, jdoerfert, 
thopre, hiraditya.
Herald added a reviewer: ftynse.
Herald added a reviewer: dcaballe.
Herald added a project: All.
domada requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added a reviewer: nicolasvasilache.
Herald added subscribers: llvm-commits, cfe-commits, jplehr, sstefan1, 
stephenneuendorffer, nicolasvasilache.
Herald added projects: clang, MLIR, LLVM.

Check if generated code can be marked as the code which does not throw an 
exception.

If LLVM IR does not throw an exception, add nounwind function attribute.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D147321

Files:
  clang/lib/CodeGen/CodeGenFunction.cpp
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  llvm/include/llvm/IR/IRBuilder.h
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  llvm/lib/IR/IRBuilder.cpp
  mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
  mlir/test/Target/LLVMIR/openmp-llvm.mlir

Index: mlir/test/Target/LLVMIR/openmp-llvm.mlir
===================================================================
--- mlir/test/Target/LLVMIR/openmp-llvm.mlir
+++ mlir/test/Target/LLVMIR/openmp-llvm.mlir
@@ -2464,7 +2464,7 @@
   }
 }
 
-// CHECK: attributes #0 = { "target-cpu"="gfx908"
+// CHECK: attributes #0 = { nounwind "target-cpu"="gfx908"
 // CHECK-SAME: "target-features"="+dot3-insts,+dot4-insts,+s-memtime-inst,
 // CHECK-SAME: +16-bit-insts,+s-memrealtime,+dot6-insts,+dl-insts,
 // CHECK-SAME: +wavefrontsize64,+gfx9-insts,+gfx8-insts,+ci-insts,+dot10-insts,
@@ -2484,7 +2484,7 @@
   llvm.return
   }
 }
-// CHECK: attributes #0 = { "target-cpu"="gfx908"
+// CHECK: attributes #0 = {  nounwind "target-cpu"="gfx908"
 // CHECK-SAME: "target-features"="+dot3-insts,+dot4-insts,+s-memtime-inst,
 // CHECK-SAME: +16-bit-insts,+s-memrealtime,+dot6-insts,+dl-insts,
 // CHECK-SAME: +wavefrontsize64,+gfx9-insts,+gfx8-insts,+ci-insts,+dot10-insts,
Index: mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
===================================================================
--- mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
@@ -1582,6 +1582,9 @@
     Operation *op, NamedAttribute attribute,
     LLVM::ModuleTranslation &moduleTranslation) const {
 
+  llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder();
+  ompBuilder->tryMarkNoThrowModuleFunctions();
+
   return llvm::TypeSwitch<Attribute, LogicalResult>(attribute.getValue())
       .Case([&](omp::TargetAttr targetAttr) {
         return setLLVMFunctionTargetAttr(op, targetAttr, moduleTranslation);
Index: llvm/lib/IR/IRBuilder.cpp
===================================================================
--- llvm/lib/IR/IRBuilder.cpp
+++ llvm/lib/IR/IRBuilder.cpp
@@ -1410,6 +1410,20 @@
   return CreateAlignmentAssumptionHelper(DL, PtrValue, Alignment, OffsetValue);
 }
 
+void IRBuilderBase::TryMarkNoThrow(llvm::Function *F) {
+  // LLVM treats 'nounwind' on a function as part of the type, so we
+  // can't do this on functions that can be overwritten.
+  if (F->isInterposable())
+    return;
+
+  for (llvm::BasicBlock &BB : *F)
+    for (llvm::Instruction &I : BB)
+      if (I.mayThrow())
+        return;
+
+  F->setDoesNotThrow();
+}
+
 IRBuilderDefaultInserter::~IRBuilderDefaultInserter() = default;
 IRBuilderCallbackInserter::~IRBuilderCallbackInserter() = default;
 IRBuilderFolder::~IRBuilderFolder() = default;
Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
===================================================================
--- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -5052,6 +5052,16 @@
   }
 }
 
+void OpenMPIRBuilder::tryMarkNoThrowModuleFunctions() {
+  for (Function &f : M.functions()) {
+    if (f.isDeclaration())
+      continue;
+    if (f.hasFnAttribute(Attribute::NoUnwind))
+      continue;
+    IRBuilderBase::TryMarkNoThrow(&f);
+  }
+}
+
 void TargetRegionEntryInfo::getTargetRegionEntryFnName(
     SmallVectorImpl<char> &Name, StringRef ParentName, unsigned DeviceID,
     unsigned FileID, unsigned Line, unsigned Count) {
Index: llvm/include/llvm/IR/IRBuilder.h
===================================================================
--- llvm/include/llvm/IR/IRBuilder.h
+++ llvm/include/llvm/IR/IRBuilder.h
@@ -2543,6 +2543,11 @@
   CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
                                       Value *Alignment,
                                       Value *OffsetValue = nullptr);
+
+  /// Tries to mark the given function nounwind based on the
+  /// non-existence of any throwing calls within it.  We believe this is
+  /// lightweight enough to do at -O0.
+  static void TryMarkNoThrow(llvm::Function *F);
 };
 
 /// This provides a uniform API for creating instructions and inserting
Index: llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
===================================================================
--- llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
+++ llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
@@ -2182,6 +2182,9 @@
   /// \param AttributeValue Value of the attribute
   void addAttributeToModuleFunctions(StringRef AttributeName,
                                      StringRef AttributeValue);
+
+  /// Try to add nounwind attribute to the generated functions
+  void tryMarkNoThrowModuleFunctions();
 };
 
 /// Class to represented the control flow structure of an OpenMP canonical loop.
Index: clang/lib/CodeGen/CodeGenFunction.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenFunction.cpp
+++ clang/lib/CodeGen/CodeGenFunction.cpp
@@ -39,6 +39,7 @@
 #include "llvm/IR/DataLayout.h"
 #include "llvm/IR/Dominators.h"
 #include "llvm/IR/FPEnv.h"
+#include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/IntrinsicInst.h"
 #include "llvm/IR/Intrinsics.h"
 #include "llvm/IR/MDBuilder.h"
@@ -1278,22 +1279,6 @@
     EmitBlock(SkipCountBB);
 }
 
-/// Tries to mark the given function nounwind based on the
-/// non-existence of any throwing calls within it.  We believe this is
-/// lightweight enough to do at -O0.
-static void TryMarkNoThrow(llvm::Function *F) {
-  // LLVM treats 'nounwind' on a function as part of the type, so we
-  // can't do this on functions that can be overwritten.
-  if (F->isInterposable()) return;
-
-  for (llvm::BasicBlock &BB : *F)
-    for (llvm::Instruction &I : BB)
-      if (I.mayThrow())
-        return;
-
-  F->setDoesNotThrow();
-}
-
 QualType CodeGenFunction::BuildFunctionArgList(GlobalDecl GD,
                                                FunctionArgList &Args) {
   const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
@@ -1491,7 +1476,7 @@
   // If we haven't marked the function nothrow through other means, do
   // a quick pass now to see if we can.
   if (!CurFn->doesNotThrow())
-    TryMarkNoThrow(CurFn);
+    llvm::IRBuilderBase::TryMarkNoThrow(CurFn);
 }
 
 /// ContainsLabel - Return true if the statement contains a label in it.  If
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D147321: [Flang][O... Dominik Adamski via Phabricator via cfe-commits

Reply via email to