@@ -826,14 +826,22 @@ cir::GlobalOp CIRGenModule::createGlobalOp(CIRGenModule &cgm,
826826 // Some global emissions are triggered while emitting a function, e.g.
827827 // void s() { const char *s = "yolo"; ... }
828828 //
829- // Be sure to insert global before the current function
829+ // Save the current function context for later insertion logic
830830 auto *curCGF = cgm.getCurrCIRGenFun ();
831- if (curCGF)
832- builder.setInsertionPoint (curCGF->CurFn );
831+
832+ // Clear insertion point to prevent auto-insertion by create()
833+ // We'll manually insert at the correct location below
834+ builder.clearInsertionPoint ();
833835
834836 g = cir::GlobalOp::create (builder, loc, name, t, isConstant, linkage,
835837 addrSpace);
836- if (!curCGF) {
838+
839+ // Manually insert at the correct location
840+ if (curCGF) {
841+ // Insert before the current function being generated
842+ cgm.getModule ().insert (mlir::Block::iterator (curCGF->CurFn ), g);
843+ } else {
844+ // Insert at specified point or at end of module
837845 if (insertPoint)
838846 cgm.getModule ().insert (insertPoint, g);
839847 else
@@ -2675,6 +2683,11 @@ void CIRGenModule::setDSOLocal(mlir::Operation *op) const {
26752683 }
26762684}
26772685
2686+ void CIRGenModule::setGVProperties (mlir::Operation *op, GlobalDecl gd) const {
2687+ assert (!cir::MissingFeatures::setDLLImportDLLExport ());
2688+ setGVPropertiesAux (op, dyn_cast<NamedDecl>(gd.getDecl ()));
2689+ }
2690+
26782691void CIRGenModule::setGVProperties (mlir::Operation *op,
26792692 const NamedDecl *d) const {
26802693 assert (!cir::MissingFeatures::setDLLImportDLLExport ());
@@ -2710,10 +2723,12 @@ cir::FuncOp CIRGenModule::createCIRFunction(mlir::Location loc, StringRef name,
27102723 // Some global emissions are triggered while emitting a function, e.g.
27112724 // void s() { x.method() }
27122725 //
2713- // Be sure to insert a new function before a current one.
2726+ // Save the current function context for later insertion logic
27142727 auto *curCGF = getCurrCIRGenFun ();
2715- if (curCGF)
2716- builder.setInsertionPoint (curCGF->CurFn );
2728+
2729+ // Clear insertion point to prevent auto-insertion by create()
2730+ // We'll manually insert at the correct location below
2731+ builder.clearInsertionPoint ();
27172732
27182733 f = cir::FuncOp::create (builder, loc, name, ty);
27192734
@@ -2739,8 +2754,14 @@ cir::FuncOp CIRGenModule::createCIRFunction(mlir::Location loc, StringRef name,
27392754 // Set the special member attribute for this function, if applicable.
27402755 setCXXSpecialMemberAttr (f, fd);
27412756
2742- if (!curCGF)
2757+ // Manually insert at the correct location
2758+ if (curCGF) {
2759+ // Insert before the current function being generated
2760+ theModule.insert (mlir::Block::iterator (curCGF->CurFn ), f);
2761+ } else {
2762+ // Insert at end of module
27432763 theModule.push_back (f);
2764+ }
27442765 }
27452766 return f;
27462767}
@@ -3064,20 +3085,27 @@ void CIRGenModule::setFunctionAttributes(GlobalDecl globalDecl,
30643085 // NOTE(cir): Original CodeGen checks if this is an intrinsic. In CIR we
30653086 // represent them in dedicated ops. The correct attributes are ensured during
30663087 // translation to LLVM. Thus, we don't need to check for them here.
3067- assert (!isThunk && " isThunk NYI" );
30683088
3069- if (!isIncompleteFunction) {
3089+ const auto *funcDecl = dyn_cast<FunctionDecl>(globalDecl.getDecl ());
3090+
3091+ if (!isIncompleteFunction)
30703092 setCIRFunctionAttributes (globalDecl,
30713093 getTypes ().arrangeGlobalDeclaration (globalDecl),
30723094 func, isThunk);
3095+
3096+ // Add the Returned attribute for "this", except for iOS 5 and earlier
3097+ // where substantial code, including the libstdc++ dylib, was compiled with
3098+ // GCC and does not actually return "this".
3099+ if (!isThunk && getCXXABI ().HasThisReturn (globalDecl) &&
3100+ !(getTriple ().isiOS () && getTriple ().isOSVersionLT (6 ))) {
3101+ llvm_unreachable (" NYI" );
30733102 }
30743103
30753104 // TODO(cir): Complete the remaining part of the function.
30763105 assert (!cir::MissingFeatures::setFunctionAttributes ());
30773106
30783107 if (!isIncompleteFunction && func.isDeclaration ())
3079- getTargetCIRGenInfo ().setTargetAttributes (globalDecl.getDecl (), func,
3080- *this );
3108+ getTargetCIRGenInfo ().setTargetAttributes (funcDecl, func, *this );
30813109
30823110 // TODO(cir): This needs a lot of work to better match CodeGen. That
30833111 // ultimately ends up in setGlobalVisibility, which already has the linkage of
0 commit comments