[clang] [CIR] Upstream simple function bodies (PR #127674)

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 18 12:00:32 PST 2025


================
@@ -0,0 +1,134 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Internal per-function state used for AST-to-ClangIR code gen
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENFUNCTION_H
+#define LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENFUNCTION_H
+
+#include "CIRGenBuilder.h"
+#include "CIRGenModule.h"
+#include "CIRGenTypeCache.h"
+
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/Decl.h"
+#include "clang/AST/Type.h"
+#include "clang/CIR/Dialect/IR/CIRDialect.h"
+#include "clang/CIR/TypeEvaluationKind.h"
+
+#include "llvm/ADT/ScopedHashTable.h"
+
+namespace clang::CIRGen {
+
+class CIRGenFunction : public CIRGenTypeCache {
+public:
+  CIRGenModule &cgm;
+
+private:
+  /// The builder is a helper class to create IR inside a function. The
+  /// builder is stateful, in particular it keeps an "insertion point": this
+  /// is where the next operations will be introduced.
+  CIRGenBuilderTy &builder;
+
+public:
+  clang::QualType fnRetTy;
+
+  /// This is the current function or global initializer that is generated code
+  /// for.
+  mlir::Operation *curFn = nullptr;
+
+  clang::ASTContext &getContext() const { return cgm.getASTContext(); }
+
+  CIRGenBuilderTy &getBuilder() { return builder; }
+
+  CIRGenModule &getCIRGenModule() { return cgm; }
+  const CIRGenModule &getCIRGenModule() const { return cgm; }
+
+  mlir::Type convertTypeForMem(QualType T);
+
+  mlir::Type convertType(clang::QualType T);
+  mlir::Type convertType(const TypeDecl *T) {
+    return convertType(getContext().getTypeDeclType(T));
+  }
+
+  ///  Return the cir::TypeEvaluationKind of QualType \c T.
+  static cir::TypeEvaluationKind getEvaluationKind(clang::QualType T);
+
+  static bool hasScalarEvaluationKind(clang::QualType T) {
+    return getEvaluationKind(T) == cir::TEK_Scalar;
+  }
+
+  CIRGenFunction(CIRGenModule &cgm, CIRGenBuilderTy &builder,
+                 bool suppressNewContext = false);
+  ~CIRGenFunction();
+
+  CIRGenTypes &getTypes() const { return cgm.getTypes(); }
+
+  mlir::MLIRContext &getMLIRContext() { return cgm.getMLIRContext(); }
+
+  /// Use to track source locations across nested visitor traversals.
+  /// Always use a `SourceLocRAIIObject` to change currSrcLoc.
+  std::optional<mlir::Location> currSrcLoc;
+  class SourceLocRAIIObject {
+    CIRGenFunction &cgf;
+    std::optional<mlir::Location> oldLoc;
+
+  public:
+    SourceLocRAIIObject(CIRGenFunction &cgf, mlir::Location value) : cgf(cgf) {
+      if (cgf.currSrcLoc)
+        oldLoc = cgf.currSrcLoc;
+      cgf.currSrcLoc = value;
+    }
+
+    /// Can be used to restore the state early, before the dtor
+    /// is run.
+    void restore() { cgf.currSrcLoc = oldLoc; }
+    ~SourceLocRAIIObject() { restore(); }
+  };
+
+  /// Helpers to convert Clang's SourceLocation to a MLIR Location.
+  mlir::Location getLoc(clang::SourceLocation SLoc);
+  mlir::Location getLoc(clang::SourceRange SLoc);
+  mlir::Location getLoc(mlir::Location lhs, mlir::Location rhs);
+
+  void finishFunction(SourceLocation EndLoc);
----------------
andykaylor wrote:

```suggestion
  void finishFunction(SourceLocation endLoc);
```

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


More information about the cfe-commits mailing list