By Anvee Bhutani

U.S. stocks advanced to close out a blockbuster quarter, marked by sharp gains in the technology sector on expectations for strong spending for artificial intelligence. The S&P 500 and Nasdaq logged their best quarterly performances since 2020.

The Dow Jones Industrial Average rose 136.46 points, or 0.26% to 52319.20 on Tuesday. The S&P 500 rose 58.93 points, or 0.79%, to 7499.36, while the Nasdaq Composite gained 393.58 points, 1.52% to 26213.72. According to preliminary data, there were 1285 advancing issues and 1477 declining issues on the NYSE.

The quarter's biggest winners were chip stocks. The PHLX Semiconductor Index climbed 88% through Tuesday, its best quarter in history, driven by names including Micron, Intel and Marvell. Astera Labs, the $78 billion chipmaker that joined the Nasdaq-100 last week, more than quadrupled. That stood in sharp contrast to some of the biggest names in technology: Microsoft fell 17% in June alone, its worst month since December 2000, at the height of the dot-com bust. Meta ended the quarter in the red.

Chip-adjacent tech stocks continued to advance Tuesday. Super Micro Computer added 4.2%, Applied Materials rose 4.1% and Sandisk gained about 11%. A consortium backed by BlackRock, Google and Coinbase announced plans to launch a new dollar-backed stablecoin called Open USD, to be offered later this year on multiple blockchain networks. Some 140 companies, including Visa, Stripe and Mastercard, have signed up to use it.

Strategy shares fell 6.2%, giving back some of Monday's surge after the bitcoin-hoarding company abandoned its longstanding never sell bitcoin mantra in a bid to shore up investor confidence.

Oil prices were little changed as investors monitored developments in Middle East peace talks in Qatar. Brent crude ended at $72.92 a barrel, losing $45.43 per barrel, or 39%, this quarter.

Traders were also on high alert for potential currency intervention in Japan, after the yen touched a 40-year low against the dollar. Tokyo has already spent more than $70 billion of foreign-exchange reserves propping up the currency this year and signaled it was ready to intervene again.

The US Dollar Index strengthened by 0.10 percentage point. The euro traded at $1.14.

Gold was little changed on the day but finished the quarter down 13%, its worst three-month stretch since 2013, pressured by expectations of Federal Reserve rate hikes. Traders are now pricing in more than a 60% chance of an increase by September, according to CME FedWatch data. Consumer confidence edged up to 91.2 in June from a downwardly revised 90.6 in May, the Conference Board said, as moderating gasoline prices offered some relief to households, though the reading came in below economist expectations of 94.2.

Air Products & Chemicals rose 8% after the industrial-gases supplier said it would abandon a troubled plan to build a low-carbon hydrogen plant in Louisiana, writing off as much as $2.9 billion. The project, unveiled in 2021, had run into environmental objections, cost overruns and a lack of buyers.

Yields on U.S. Treasury bonds rose. The 10-year yield increased 0.045 percentage point to 4.420%.

Looking ahead, Fed Chairman Kevin Warsh is expected to speak Wednesday at the European Central Bank Forum in Portugal. Thursday brings the June jobs report, which investors will parse closely for clues about the Fed's next move on rates. Markets are closed Friday in observance of Independence Day.

Write to Anvee Bhutani at anvee.bhutani@wsj.com