HONG KONG/TAIPEI, July 21 (Reuters) - Investors are putting aside geopolitical tensions to pile in to Taiwan stocks, with foreign inflows the biggest in years, thanks to soaring artificial intelligence and chipmaking stocks.

The wave of enthusiasm, which has also helped the tech-heavy Nasdaq to its best first half in 40 years, is running hard as the market is dominated like no other by top firms all along the computer hardware and software supply chains.

Net foreign buying of $12 billion over the six months to June is the biggest since the first half of 2008, and Taiwan's benchmark index performance - it is up 20% in U.S. dollar terms this year - is the best in Asia.

The sparkling rally has scarcely been dented by Taiwan's slowing economy, repeated shows of force by the Chinese military, or while war in Ukraine highlights what is at stake over the Taiwan Strait.

Rather, investors say it's all the more sturdy as the bogged-down conflict deters Chinese action and risks can be managed by keeping positions liquid with one eye on a possibly quick exit.

And, market watchers, say the AI rally has further to go.

"What we are seeing now is a tactical trade, which tends to be based on a shorter-term investment horizon before the tech sector's valuation peak," said Carlos Casanova, Asia senior economist at Union Bancaire Privee.

"A potential escalation of events in the Taiwan Strait down the line is less relevant for these investors," he said.

China claims democratically governed Taiwan as its own territory, a view strongly contested by the government in Taipei.

China staged war games around Taiwan last August and again in April, and it has regularly flown military aircraft across the median line of the Taiwan Strait, which had previously served as an unofficial barrier between the two.

Famous investor Warren Buffett called geopolitical tensions "a consideration" in his decision to last year sell most of a stake in the giant Taiwan Semiconductor Manufacturing Co just a few months after buying it.

"Nobody can say they are unconcerned about the military threat from China," said one funds management veteran in Taiwan, who spoke on condition of anonymity in order to talk freely.

"However, we've been living with these cross-strait tensions for a long time," he said. "The entire AI supply chain is in Taiwan, and the entry barriers for any potential competitors are quite high.

"So that makes Taiwan’s AI supply chain very attractive for foreign investors, and we believe their fund inflows will continue to be strong throughout the year."

'CLEAR WINNER'

Artificial intelligence stocks have soared this year after products such as ChatGPT demonstrated it can research and prepare fully formed sentences, fuelling bets on its potential to do all sorts of labour-saving and creative tasks.

Investors say Taiwan's market is uniquely positioned to benefit because it is exposed to the sector's growth from applications to components, where demand is rebounding.

"For the turnaround expected for semiconductor sector, the clear winner in Asia is Taiwan and will likely remain so," said Frank Benzimra, head of Asia equity strategy at Societe Generale.

Hardware makers that will benefit include circuit board maker Unimicron, network switch maker Accton Technology and contract electronics maker Hon Hai Precision Industry, according to brokerage KGI Asia.

Share prices for Wistron Corp ,which focuses on server assembling, laptop maker Quanta Computer; and chip design firms Alchip Technologies and Global Unichip have all more than doubled this year.

"We are at the start in the ongoing AI revolution," said Andrew Swan, head of Asia equities excluding Japan at hedge fund Man GLG, which has been adding exposure this year.

Cathie Wood's Ark Investment Management has also been buying TSMC in June, while Vanguard and PineBridge Investments Asia also added exposure in Alchip last quarter, according to company filings and media reports.

To be sure, not everyone is bullish and with exports and the domestic economy pressured by faltering demand Goldman Sachs has noted that further foreign flows might need earnings to pick up.

There is also a presidential election set for January 2024 that is likely to draw tension and politics to the surface -- but investors are regarding worries as manageable.

"We don't see any imminent risks of the cross-strait relations deteriorating into a war," said Clarence Chan, Asia Pacific ex-Japan head of client solutions at $1.9 trillion BNY Mellon Investment Management, which has added to Taiwan stocks.

"The Chinese government is dealing with other more immediate priorities such as youth unemployment, and economic recovery from COVID, than Taiwan." (Writing by Tom Westbrook; Additional reporting by Summer Zhen in Hong Kong; Editing by Kim Coghill)